32

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.

Sergiy
  • 1,807
  • 2
  • 16
  • 23
  • 1
    This is a good and working, example (MVC 6) and lib of implementation with ASP.NET 5 Identity (>= v3) framework without Entity Framework for MongoDB.Driver (>= v2.1.0) https://github.com/saan800/SaanSoft.AspNet.Identity3.MongoDB – Stanislav Prusac Sep 20 '15 at 13:49
  • 25
    This is specific question about a common problem, that changes with each version of Identity: How do you surgically remove EF from Identity so you can use other data access technologies. It has nothing to do with recommending a tool. @Martijn Pieters did you even read the question before closing it? Reopen this question urgently ! – bbsimonbb Mar 09 '16 at 09:49

2 Answers2

13

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>();
Joe Audette
  • 28,887
  • 11
  • 87
  • 93
  • 1
    Thank you. We do the similar way in our solution. – Sergiy Aug 05 '15 at 15:28
  • how do you solve the fact that it doesn't use a transaction? – gilmishal Jun 30 '16 at 18:49
  • @gilmishal, I believe the UserStore implementation should call your non-EF repository implementation. This repository could use transactions if it participates in a unit of work, which may tie the start of the transaction to the beginning of the action, and the end of the transaction to the end of the business operation before returning the action result. – Jorge Yanes Diez Jul 22 '16 at 12:19
  • And in Microsoft.AspNetCore.Identity 1.0.0 there are extensionmethods to configure your own stores: `services.AddIdentity() .AddRoleStore() .AddUserStore();` myAppRoleStore must implement IRoleStore and myAppUserStore must impement IUserStore – Kirsten Aug 09 '16 at 10:54
3

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

Since ASP.NET Identity 3 is part of the .NET Framework 5, which is still unreleased, my guess is you won't find any examples.

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.

Again, the lack of docs is probably due to the unreleased nature of the software. However just looking at the source code, it seems as though the ApplicationUser can derive from any POCO object -- without the need to implement an IUser<TKey> interface.

As far as configuring services, have a look at IdentityServiceCollectionExtensions and IdentityEntityFrameworkBuilderExtensions. It seems as if the first is in identity core as a means of providing a context within which to register services for application identity, whereas the second is an entityframework-specific implementation using that context.

The solution for implementing something that uses ASP.NET Identity 3 but not EF seems like it would just be a matter of providing different implementations for the identity service interfaces and then wiring up those dependencies during app configuration. You can use the base EntityFramework implementation as a guide for how to DIY. But caveat emptor, identity 3 could change again before final release, so anything you build against identity 3 now is subject to change.

danludwig
  • 45,241
  • 21
  • 150
  • 230
  • Even with .NET 5 released now, there is still very little documentation on separating EF from Identity. We really want to use Identity framework, but also want to leverage the power of Stored Procedures as well as custom User, Role, and Grouping mechanisms. – spencer741 Jan 30 '21 at 19:57