33

我现在看到的所有 ASP.NET Identity 3.0 示例都使用实体框架来存储与用户相关的数据。

是否有任何不使用实体框架且ApplicationUser类不是从哪里派生的示例Microsoft.AspNet.Identity.EntityFramework.IdentityUser

在 ASP.NET Identity 2.x 中,需要实现IUser接口。现在似乎没有这样的接口 - 所以我们不确定如何User正确定义类。几乎没有关于这个主题的文档。

第二个问题是AddIdentity调用Startup.ConfigureServices。它与命名空间中的特定类非常相关,Microsoft.AspNet.Identity.EntityFramework并且不清楚如何在没有这些类的情况下注册身份服务。

4

2 回答 2

13

我已经在我的项目中实现了,你要实现的主要是 UserStore 和 RoleStore

我的 SiteUser 和 SiteRole 类不继承任何东西

主要是在让asp.net身份添加自己的服务之前添加自己的服务

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

一些相同的接口将在此处注册,但如果它们先注册,它将使用您的接口

services.AddIdentity<SiteUser, SiteRole>();
于 2015-08-04T15:52:49.777 回答
3

是否有任何不使用 EntityFramework 且 ApplicationUser 类不是从 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 派生的示例?

由于 ASP.NET Identity 3 是尚未发布的 .NET Framework 5 的一部分,我猜你不会找到任何示例。

在 ASP.NET Identity 2.x 中,需要实现 IUser 接口。现在似乎没有这样的接口 - 所以我们不确定如何正确定义“用户”类。几乎没有关于这个主题的文档。

同样,缺少文档可能是由于该软件的未发布性质。但是,仅查看源代码,似乎 ApplicationUser 可以派生自任何 POCO 对象——无需实现IUser<TKey>接口。

至于配置服务,请查看IdentityServiceCollectionExtensionsIdentityEntityFrameworkBuilderExtensions。似乎第一个在身份核心中作为一种提供上下文的方法,在该上下文中为应用程序身份注册服务,而第二个是使用该上下文的特定于实体框架的实现。

实现使用 ASP.NET Identity 3 而不是 EF 的解决方案似乎只是为身份服务接口提供不同的实现,然后在应用程序配置期间连接这些依赖项。您可以使用基本的 EntityFramework 实现作为如何 DIY 的指南。但是需要注意的是,身份 3 可能会在最终发布之前再次更改,因此您现在针对身份 3 构建的任何内容都可能会发生更改。

于 2015-08-03T20:32:29.297 回答