Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving ASP.NET Identity model to class library

I am trying to move the Identity model to a class library using the methods in this link:

ASP.NET Identity in Services library

Problem 1: It seems to keep using the Website project's connection string. I overcame it by specifying the full connection string in the class library. Can I make the IdentityDbContext use the class library's connection string?

Problem 2: Due to the problem 1, if I remove the Entity Framework from the website project. It will give the following error that it is looking for EF's SqlClient in the Website project.

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

Other solutions are welcome as long as it omits all Data Access Layer references like EF in the Website project.

like image 925
Joe Yap Avatar asked May 03 '14 16:05

Joe Yap


Video Answer


2 Answers

To move the IdentityModel into a class library (which is the right thing to do according to the SRP), follow these steps:

  1. Create a class library. (ClassLibrary1)
  2. Using NuGet, add a reference to Microsoft.AspNet.Identity.EntityFramework. This will also auto-add some other references.
  3. Add a reference in your website to ClassLibrary1
  4. Find WebSite/Models/IdentityModel.cs and move it to ClassLibrary1.
  5. Make IdentityModel.cs look like this:

    public class ApplicationUser : IdentityUser { }  public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {     public ApplicationDbContext()         : base("YourContextName")     {     } } 
  6. Make sure your website's Web.config has YourContextName pointing to the right database in the section. (Note: this database can and should house your application data).

    <add name="YourContextName" connectionString="YourConnectionStringGoesHere"   providerName="System.Data.SqlClient" /> 
  7. Make your EF Context class inherit from your ApplicationDbContext:

    public class YourContextName : ApplicationDbContext {     public DbSet<ABizClass1> BizClass1 { get; set; }     public DbSet<ABizClass2> BizClass2 { get; set; }     // And so forth ... } 

When anyone in your site tries to log in or register, the Identity system will route them to your database with all your data which includes the Identity tables.

Good to go!

like image 194
Rap Avatar answered Sep 28 '22 20:09

Rap


An update to @Rap's answer for EF6 and Identity 2.0:

  1. Create a class library. (ClassLibrary1)
  2. Using NuGet, add a reference to Microsoft.AspNet.Identity.EntityFramework and Microsoft.AspNet.Identity.Owin.
  3. Add a reference in your website to ClassLibrary1
  4. Find WebSite/Models/IdentityModel.cs and move it to ClassLibrary1.
  5. IdentityModel.cs should look like this, no need to change anything:

    public class ApplicationUser : IdentityUser {     public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)     {         // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType         var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);         // Add custom user claims here         return userIdentity;     } }  public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {     public ApplicationDbContext()         : base("DefaultConnection", throwIfV1Schema: false)     {     }      public static ApplicationDbContext Create()     {         return new ApplicationDbContext();     } } 
  6. Make sure your website's Web.config has a context pointing to the right database in the section. (Note: this database can and should house your application data).

    <connectionStrings>   <add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=Project;Integrated Security=sspi;Pooling=false;" providerName="System.Data.SqlClient" /> </connectionStrings> 
  7. Make your EF Context class inherit from your ApplicationDbContext:

    public class YourContextName : ApplicationDbContext {     public DbSet<ABizClass1> BizClass1 { get; set; }     public DbSet<ABizClass2> BizClass2 { get; set; }     // And so forth ... } 
like image 27
Ogglas Avatar answered Sep 28 '22 18:09

Ogglas