[英]Using a Custom UserStore and RoleStore in ASP.NET 5
I have implemented a custom RoleStore and a custom UserStore for my project that is using ASP.NET 5, MVC 6, EF 7, and Identity 3. However - I can't quite figure out how to configure identity to use my custom RoleStore and custom UserStore instead of the usual offering.我已经为我使用 ASP.NET 5、MVC 6、EF 7 和 Identity 3 的项目实现了一个自定义 RoleStore 和一个自定义 UserStore。但是 - 我不太清楚如何配置身份以使用我的自定义 RoleStore 和自定义 UserStore 而不是通常的产品。 How can I reconfigure the system to use my custom classes?
如何重新配置系统以使用我的自定义类?
PS: I also have custom User and Role class. PS:我也有自定义的 User 和 Role 类。
Here's what I ended up doing.这就是我最终做的。 First, I uninstalled the 'Identity Entity Framework' package from my project.
首先,我从我的项目中卸载了“身份实体框架”包。 This sent a few things missing, so I re-implemented them (read: copied them from here ), and put them in a 'Standard' namespace to indicate they hadn't been customised.
这让一些东西丢失了,所以我重新实现了它们(阅读:从这里复制它们),并将它们放在一个“标准”命名空间中,以表明它们没有被定制。 I now have a 'Security' namespace that contains the following:
我现在有一个包含以下内容的“安全”命名空间:
The items shown in bold contain project specific functionality.以粗体显示的项目包含项目特定的功能。
The code that allows me to use the custom stores is in the 'BuilderExtensions' file, which contains the following class:允许我使用自定义存储的代码位于“BuilderExtensions”文件中,该文件包含以下类:
public static class BuilderExtensions
{
public static IdentityBuilder AddCustomStores<TContext, TKey>(this IdentityBuilder builder)
where TContext : DbContext
where TKey : IEquatable<TKey>
{
builder.Services.TryAdd(GetDefaultServices(builder.UserType, builder.RoleType, typeof(TContext), typeof(TKey)));
return builder;
}
private static IServiceCollection GetDefaultServices(Type userType, Type roleType, Type contextType, Type keyType)
{
var userStoreType = typeof(UserStore<,,,>).MakeGenericType(userType, roleType, contextType, keyType);
var roleStoreType = typeof(RoleStore<,,>).MakeGenericType(roleType, contextType, keyType);
var services = new ServiceCollection();
services.AddScoped(
typeof(IUserStore<>).MakeGenericType(userType),
userStoreType);
services.AddScoped(
typeof(IRoleStore<>).MakeGenericType(roleType),
roleStoreType);
return services;
}
}
This then allows me to write the following in my Startup.cs file:这允许我在 Startup.cs 文件中写入以下内容:
services.AddIdentity<User, Role>()
.AddCustomStores<PrimaryContext, string>()
.AddDefaultTokenProviders();
And the custom store will be used.并且将使用自定义商店。 Note that PrimaryContext is the name of my whole-project DbContext.
请注意,PrimaryContext 是我的整个项目 DbContext 的名称。 it inherits from IdentityDbContext.
它继承自 IdentityDbContext。
I could have probably kept the 'Identity Entity Framework' package and saved myself duplicating the contents of the 'Standard' namespace, but I chose not to so that I can keep my identifiers short and unambiguous.我本可以保留“身份实体框架”包并避免重复“标准”命名空间的内容,但我选择不这样做,以便我可以保持我的标识符简短和明确。
Check out this section Reconfigure application to use new storage provider in Overview of Custom Storage Providers for ASP.NET Identity查看此部分重新配置应用程序以使用ASP.NET 身份的自定义存储提供程序概述中的新存储提供程序
Specifically "If the default storage provider was included in your project, you must remove the default provider and replace it with your provider."特别是“如果您的项目中包含默认存储提供程序,您必须删除默认提供程序并将其替换为您的提供程序。”
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new YourNewUserStore(context.Get<ExampleStorageContext>()));
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.