ASP.NET Core - custom AspNetCore.Identity implementation not working

前端 未结 1 387
情歌与酒
情歌与酒 2021-01-06 06:08

I\'m building a completely custom AspNetCore.Identity Implementation because I want TKey to be System.Guid across the board. With respect, I have d

相关标签:
1条回答
  • 2021-01-06 06:36

    Your ApplicationUserStore needs the RoleClaim at the end too (don't forget to update the related NuGet packages, otherwise you can't use these new additions):

        ApplicationUserStore : UserStore<
                User, Role, ApplicationDbContext, 
                Guid, UserClaim, UserRole, 
                UserLogin, UserToken, RoleClaim>
    

    Plus your ApplicationRoleStore should provide how to create the RoleClaim,

    protected override RoleClaim CreateRoleClaim(Role role, Claim claim)
    {
        return new RoleClaim
        {
            RoleId = role.Id,
            ClaimType = claim.Type,
            ClaimValue = claim.Value
        };
    }
    

    And also the ApplicationUserStore should provide these mappings too:

    protected override UserClaim CreateUserClaim(User user, Claim claim)
    {
        var userClaim = new UserClaim { UserId = user.Id };
        userClaim.InitializeFromClaim(claim);
        return userClaim;
    }
    
    protected override UserLogin CreateUserLogin(User user, UserLoginInfo login)
    {
        return new UserLogin
        {
            UserId = user.Id,
            ProviderKey = login.ProviderKey,
            LoginProvider = login.LoginProvider,
            ProviderDisplayName = login.ProviderDisplayName
        };
    }
    
    protected override UserRole CreateUserRole(User user, Role role)
    {
        return new UserRole
        {
            UserId = user.Id,
            RoleId = role.Id
        };
    }
    
    protected override UserToken CreateUserToken(User user, string loginProvider, string name, string value)
    {
        return new UserToken
        {
            UserId = user.Id,
            LoginProvider = loginProvider,
            Name = name,
            Value = value
        };
    }
    

    Then redirect built-in services to your custom services:

    services.AddScoped<UserStore<User, Role, ApplicationDbContext, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>, ApplicationUserStore>();
    services.AddScoped<UserManager<User>, ApplicationUserManager>();
    services.AddScoped<RoleManager<Role>, ApplicationRoleManager>();
    services.AddScoped<SignInManager<User>, ApplicationSignInManager>();
    services.AddScoped<RoleStore<Role, ApplicationDbContext, int, UserRole, RoleClaim>, ApplicationRoleStore>();
    services.AddScoped<IEmailSender, AuthMessageSender>();
    services.AddScoped<ISmsSender, AuthMessageSender>();
    

    now introduce your custom services:

    services.AddIdentity<User, Role>(identityOptions =>
                {
                 // ...
                }).AddUserStore<ApplicationUserStore>()
                  .AddUserManager<ApplicationUserManager>()
                  .AddRoleStore<ApplicationRoleStore>()
                  .AddRoleManager<ApplicationRoleManager>()
                  .AddSignInManager<ApplicationSignInManager>()
                  // You **cannot** use .AddEntityFrameworkStores() when you customize everything
                  //.AddEntityFrameworkStores<ApplicationDbContext, int>()
                  .AddDefaultTokenProviders();
    
    0 讨论(0)
提交回复
热议问题