How to use ASP.NET Identity 3.0 without Entity Framework [closed]

梦想与她 提交于 2019-11-30 13:41:22

问题


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.


回答1:


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>();



回答2:


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.



来源:https://stackoverflow.com/questions/31795792/how-to-use-asp-net-identity-3-0-without-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!