简体   繁体   English

在 ASP.NET 5 中使用自定义 UserStore 和 RoleStore

[英]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 类。

Solution解决方案

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:我现在有一个包含以下内容的“安全”命名空间:

  • Standard标准
    • IdentityRole.cs身份角色
    • IdentityRoleClaim.cs IdentityRoleClaim.cs
    • IdentityUser.cs身份用户.cs
    • IdentityUserClaim.cs IdentityUserClaim.cs
    • IdentityUserLogin.cs IdentityUserLogin.cs
    • IdentityUserRole.cs身份用户角色.cs
  • BuilderExtensions.cs BuilderExtensions.cs
  • IdentityDbContext.cs IdentityDbContext.cs
  • Resources.resx资源.resx
  • Role.cs角色.cs
  • RoleStore.cs角色存储库
  • User.cs用户.cs
  • UserStore.cs用户存储库

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。

Discussion讨论

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.

相关问题 如何模拟自定义 UserStore 和 RoleStore - How to mock custom UserStore and RoleStore 具有自定义UserStore的Asp.Net身份,无法访问重载 - Asp.Net Identity with custom UserStore, overload inaccessible 自定义ASP.NET Identity 2.0 UserStore - 是否实现了所需的所有接口? - Custom ASP.NET Identity 2.0 UserStore - Is implementing all interfaces required? 如何在 ASP.NET Core 中实现自定义 UserStore(或自定义身份验证)(不是旧版本!) - How to implement Custom UserStore (or custom authentication) in ASP.NET Core (not older versions!) ASP.NET MVC:控制器未捕获自定义Identity UserStore类中引发的异常 - ASP.NET MVC: Controller not catching exception thrown in custom Identity UserStore class ASP.NET Identity UserStore:为什么要返回Task? - ASP.NET Identity UserStore: Why return Task? ASP.NET Identity UserStore中的GetNormalizedUserNameAsync和SetNormalizedUserNameAsync函数的说明 - Explanation of GetNormalizedUserNameAsync and SetNormalizedUserNameAsync functions in ASP.NET Identity UserStore ASP.NET 无法访问扩展用户存储 - ASP.NET Can't access extended UserStore ASP.NET Identity 中 UserStore 和 UserManager 的使用有什么区别? - What is the difference in the use of UserStore and UserManager in ASP.NET Identity? 为什么在ASP.NET Identity的`UserStore`中有这么多的存储库? - Why so many repositories in ASP.NET Identity's `UserStore`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM