简体   繁体   中英

How to set up Identity in an ASP.Net core project using EF 6.0 in a separate class library

A while ago I asked this question about using EF with ASP.Net core , and this is working great. Now that I want to add ASP.Net Identity, I'm wondering if the best approach is to add identity logic to the separate project with regular / non-core EF, or into the main core project with EF core, or something else. I see a lot of pages that discuss EF core, but I would prefer to use the same context everywhere, and I would prefer to use regular EF as I am currently doing. I'm looking for what would be a clean / best practices approach here, I have been using regular EF since to my knowledge EF core 1.0 doesn't support stored procs, and I prefer to continue to use a database first approach anyway. Appreciate any help that gets me out of the quicksand.

NOTE: My 'User' table (called something else) has some existing profile data in it; I intend to use this as my Identity User table.

Have you considered having an explicit Identity service that can issue tokens based on your existing identity database?

IdentityServer4 is an OpenId Connect provider. It supports EF so that you can use your existing user data to perform authentication.

The push now is towards .NET Core and it would be a good step in that direction to have a separate project dedicated to handling identity that uses IdentityServer4 and EF Core.

ASP.Net Identity Framework is designed for Code-first, and will normally try to generate its tables for you. I'm not sure if it is able to work Database-first, but I think you could get it working as follows:

  1. Create a separate, temporary, project/solution with Identity / Individual User Account.
  2. Insert the data structure from this temporary database into your main database, merging AspNetUsers with your User table.
  3. Delete the temporary project/solution and database.
  4. Update the models in your EF6 project. This should bring in the new tables.
  5. Add Identity Framework to your ASP.Net Core project, referencing the database context from your EF6 project.

I haven't tried this, but having worked with Identity Framework quite a bit I can't see a reason why it wouldn't work.

Further reading:

Database first Identity: Using Asp.Net Identity DataBase first approach

Custom table name for Users table: How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM