Skip to content

esha/NHibernate.AspNetCore.Identity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NHibernate.AspNetCore.Identity

ASP.NET Core 2.0 Identity provider that uses NHibernate for storage

Purpose

ASP.NET Core 2.0 shipped with yet another rewrite of the Identity system (in the Microsoft.AspNetCore.Identity package) in order to support both local login and remote logins via OpenID/OAuth, but only ships with an Entity Framework provider (Microsoft.AspNetCore.Identity.EntityFrameworkCore). The changes were significant enough to require another project since NHibernate.AspNet.Identity is incompatible with the new classes.

Features

  • Drop-in replacement ASP.NET Core 2.0 Identity with NHibernate for persistence.
  • Based on same schema required by Microsoft.AspNetCore.Identity.EntityFrameworkCore for model compatibility.
  • Contains the same IdentityUser class used by the EntityFramework provider in the EntityFrameworkCore project.
  • Supports additional profile properties on your application's user model.
  • Provides UserStore<TUser> implementation that implements the same interfaces as the EntityFramework version:
    • IUserStore<TUser>
    • IUserLoginStore<TUser>
    • IUserRoleStore<TUser>
    • IUserClaimStore<TUser>
    • IUserLoginStore<TUser>
    • IUserPasswordStore<TUser>
    • IUserSecurityStampStore<TUser>

Quick-start guide

These instructions assume you know how to set up NHibernate within an MVC application.

  1. Create a new ASP.NET Core 2.0 project, choosing the Individual User Accounts authentication type.
  2. Remove the Entity Framework packages and replace with NHibernate Identity:
Uninstall-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore
Uninstall-Package EntityFramework
Install-Package NHibernate.AspNetCore.Identity
  1. In ~/Models/IdentityModels.cs: a. Remove the namespace: Microsoft.AspNetCore.Identity.EntityFrameworkCore a. Add the namespace: NHibernate.AspNetCore.Identity a. Remove the ApplicationDbContext and ApplicationUserContext classes completely.
  2. In ~/Controllers/AccountController.cs a. Remove the namespace: Microsoft.AspNetCore.Identity.EntityFrameworkCore a. Add the relevant ISession implementation that will be used by default. This could be from a DI implementation. Note: This isn't mandatory, if you are using a framework that will inject the dependency, you shouldn't need the parameterless constructor.
  3. Setup configuration code

NHibernate

  // this assumes you are using the default Identity model of "ApplicationUser"
  var myEntities = new [] {
      typeof(ApplicationUser)
  };

  var configuration = new Configuration();
  configuration.Configure("sqlite-nhibernate-config.xml");
  configuration.AddDeserializedMapping(MappingHelper.GetIdentityMappings(myEntities), null);

  var factory = configuration.BuildSessionFactory();
  var session = factory.OpenSession();

  var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(session);

FluentNHibernate

  // this assumes you are using the default Identity model of "ApplicationUser"
  var myEntities = new [] {
      typeof(ApplicationUser)
  };

  var configuration = Fluently.Configure()
     .Database(/*.....*/)
     .ExposeConfiguration(cfg => {
         cfg.AddDeserializedMapping(MappingHelper.GetIdentityMappings(myEntities), null);
  });

  var factory = configuration.BuildSessionFactory();
  var session = factory.OpenSession();

  var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(session);

Thanks To

Special thanks to Antônio Milesi Bastos whos NHibernate.AspNet.Identity project gave me the base for jumpstarting the NHibernate ASP.NET Core 2.0 provider