本文介绍了AspNetCore.Identity自定义实现不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个完全自定义的AspNetCore.Identity实现,因为我想要 TKey 成为 System.Guid 董事会。关于我,我有派生类型...

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


  • 角色:IdentityRole&Guid; UserRole,RoleClaim> / code>

  • RoleClaim:IdentityRoleClaim< Guid>

  • User:IdentityUser&Guid,UserClaim,UserRole,UserLogin>

  • UserClaim:IdentityUserClaim&Guid; gt;

  • UserLogin:IdentityUserLogin&Guid>

  • UserRole :IdentityUserRole< Guid>

  • UserToken:IdentityUserToken< Guid>




  • ApplicationDbContext:IdentityDbContext< User,Role,Guid,UserClaim,UserRole,UserLogin,RoleClaim,UserToken>




  • ApplicationRoleManager:RoleManager< Role> li>
  • ApplicationRoleStore:RoleStore< Role,ApplicationDbContext,Guid,UserRole,RoleClaim>

  • ApplicationSignInManag er:SignInManager< User>

  • ApplicationUserManager:UserManager< User>

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

  • Role : IdentityRole<Guid, UserRole, RoleClaim>
  • RoleClaim : IdentityRoleClaim<Guid>
  • User : IdentityUser<Guid, UserClaim, UserRole, UserLogin>
  • UserClaim : IdentityUserClaim<Guid>
  • UserLogin : IdentityUserLogin<Guid>
  • UserRole : IdentityUserRole<Guid>
  • UserToken : IdentityUserToken<Guid>

  • ApplicationDbContext : IdentityDbContext<User, Role, Guid, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>

  • ApplicationRoleManager : RoleManager<Role>
  • ApplicationRoleStore : RoleStore<Role, ApplicationDbContext, Guid, UserRole, RoleClaim>
  • ApplicationSignInManager : SignInManager<User>
  • ApplicationUserManager : UserManager<User>
  • **ApplicationUserStore** : UserStore<User, Role, ApplicationDbContext, Guid, UserClaim, UserRole, UserLogin, UserToken>

ApplicationUserStore 是问题孩子!

实施

namespace NewCo.Identity
{
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using System;

    public sealed class Role : IdentityRole<Guid, UserRole, RoleClaim>
    {
    }
}

namespace NewCo.Identity
{
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using System;

    public sealed class UserRole : IdentityUserRole<Guid>
    {
    }
}

namespace NewCo.Identity
{
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using System;

    public sealed class RoleClaim : IdentityRoleClaim<Guid>
    {
    }
}

// The problem is here...

namespace NewCo.Identity
{
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using System;
    using System.Security.Claims;

    public sealed class ApplicationUserStore : UserStore<User, Role, ApplicationDbContext, Guid, UserClaim, UserRole, UserLogin, UserToken>
    {
    }
}

strong>

Error

据我所见,除非这是一些(co / contra / in)差异问题,所有代码检查...我怎么弄错了?

As far as I can see, unless this is some (co/contra/in)variance issue, all the code checks out...what did I get wrong?

推荐答案

您的 ApplicationUserStore 需要 RoleClaim 最后(不要忘记更新相关的NuGet软件包,否则您不能使用这些新增功能):

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>

加你的 ApplicationRoleStore 应该提供如何创建 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
    };
}

还有$ code ApplicationUserStore 应该提供这些映射:

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

这篇关于AspNetCore.Identity自定义实现不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:49