How to use ASP.NET Identity 3.0 without Entity Framework

Sergiy picture Sergiy · Aug 3, 2015 · Viewed 11k times · Source

All examples I've seen by now for ASP.NET Identity 3.0 use Entity Framework to store user-related data.

Are there any example which does not use Entity Framework and where ApplicationUser class is not derived from Microsoft.AspNet.Identity.EntityFramework.IdentityUser?

In ASP.NET Identity 2.x it was needed to implement IUser interface. It seems there is not such interface now - so we're not sure how to define User class correctly. There is almost no documentation on this subject.

Second problem is with AddIdentity call in Startup.ConfigureServices. It's pretty tied to the particular classes from Microsoft.AspNet.Identity.EntityFramework namespace and it's unclear how to register identity services without those classes.

Answer

Joe Audette picture Joe Audette · Aug 4, 2015

I have implemented it in my project, the main things you have to implement is UserStore and RoleStore

my SiteUser and SiteRole classes do not inherit from anything

the main thing is to add your own services before letting asp.net identity add its own services

services.TryAdd(ServiceDescriptor.Scoped<IUserStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserPasswordStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserEmailStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserLoginStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserRoleStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserClaimStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserPhoneNumberStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserLockoutStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserTwoFactorStore<SiteUser>, UserStore<SiteUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IRoleStore<SiteRole>, RoleStore<SiteRole>>());

some of the same interfsaces will be registered here but it will use yours if they are registered first

services.AddIdentity<SiteUser, SiteRole>();