3

I'm trying to use authentication in an ASP.NET Core project using Angular. The problem is that we have a database full of tables that are already in use. In particular, we have a User table and a UserTypes table which is basically our 'Roles.' I would like to use IdentityServer since that seems to be the default option when creating a new project. I have used the following command in Package Manager Console:

Scaffold-DbContext "Server=-----;Database=MP_Administration;Persist Security Info=True;User id=sa; Password=------" 
         Microsoft.EntityFrameworkCore.SqlServer 
         -OutputDir Models -Context MpContext -t User, UserType

It works fine but in my Startup.cs I'm struggling to make things work. I would like to do something like this:

services.AddDbContext<MpContext>(options =>
         options.UseSqlServer(Configuration.GetConnectionString("MPAdministrationDatabase")));

services.AddDefaultIdentity<User>()
        .AddEntityFrameworkStores<MpContext>();

services.AddIdentityServer()
        .AddApiAuthorization<User, MpContext>();

services.AddAuthentication()
        .AddIdentityServerJwt();

But the Identity Server doesn't seem to like the MpContext (which implements DbContext) generated by the scaffolding command mentioned earlier. I've tried writing my own versions of ApiAuthorizationDbContext which would take the place of DbContext, as well as KeyApiAuthorizationDbContext, but I'm slowly starting to feel like I'm sinking. Any help would be greatly appreciated.

David Liang
  • 20,385
  • 6
  • 44
  • 70
McCrockett
  • 159
  • 1
  • 9
  • 1
    I could never find an answer to this question. I did, however, find an acceptable alternative. This [post](https://stackoverflow.com/a/49047358/1809219) was basically what I did. However, I used Jwt authentication as opposed to Cookie. Works great! I hope someone finds a good solution to the original problem though. – McCrockett Feb 20 '20 at 23:02

0 Answers0