AspNet Identity Core - 登录时的自定义声明

AspNet Identity Core - Custom Claims on Login

我正在尝试通过在数据库中添加逻辑 'deleted' 列来扩展我的身份用户。

然后我想使用此值通过自定义 UserClaimsPrincipalFactory 向用户添加声明。

我想检查 'Deleted' 登录声明,如果用户的帐户已被删除,我会拒绝该用户。

问题: 当我尝试通过 User.Claims 访问声明时,用户没有声明。

我唯一能让它工作的方法是覆盖 httpcontext 用户

public class ApplicationClaimsIdentityFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
    private readonly IHttpContextAccessor _httpContext;

    public ApplicationClaimsIdentityFactory(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, IOptions<IdentityOptions> options, IHttpContextAccessor httpContext) : base(userManager, roleManager, options)
    {
        _httpContext = httpContext;
    }

    public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
    {
        ClaimsPrincipal principal = await base.CreateAsync(user);

        ClaimsIdentity claimsIdentity = (ClaimsIdentity) principal.Identity;

        claimsIdentity.AddClaim(new Claim("Deleted", user.Deleted.ToString().ToLower()));

        //I DON'T WANT TO HAVE TO DO THIS
        _httpContext.HttpContext.User = principal;


        return principal;
    }
}

登录操作:

public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {

            SignInResult result = await _signInManager.PasswordSignInAsync(model.Email, model.Password,
                model.RememberMe, lockoutOnFailure: false);

            if (result.Succeeded)
            {
                //No claims exists at this point unless I force the HttpContext user (See above)
                if (User.Claims.First(x => x.Type == "Deleted").Value.Equals("true", StringComparison.CurrentCultureIgnoreCase);)
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    await _signInManager.SignOutAsync();
                    return View(model);
                }

          .... Continue login code...

我的应用程序用户class

public class ApplicationUser : IdentityUser
{
    public bool Deleted { get; set; }
}

最后是我的启动注册

services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<DbContext>()
            .AddClaimsPrincipalFactory<ApplicationClaimsIdentityFactory>()
            .AddDefaultTokenProviders();

谢谢

我认为问题是您在发布登录操作时试图在一个请求中完成所有操作。

您在该方法中拥有的 User 是 claimsprincipal 但未经过身份验证,在调用 SignIn 代码之前和调用 claimsprincipal 工厂方法之前,它已由 auth 中间件从请求中反序列化。

signin 方法确实创建了新的经过身份验证的 claimsprincipal 并且应该将其序列化到 auth cookie 中,因此在下一个请求中,用户将从 cookie 中反序列化并进行身份验证,但是当前的反序列化已经发生要求。因此,为当前请求更改它的唯一方法是在您找到的当前 httpcontext 上重置用户。

我认为最好以不同的方式拒绝用户,而不是在登录成功后,最好在较低级别检查并使登录失败,而不是成功然后注销。我在自定义用户存储区的项目中执行了此操作。

在 PasswordSignInAsync 方法之后声明不可用。一种解决方法是您可以使用它在构造函数中添加 UserManager,例如:

    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly ILogger<LoginModel> _logger;
    private readonly UserManager<ApplicationUser> _userManager; //<----here

    public LoginModel(SignInManager<ApplicationUser> signInManager, 
        UserManager<ApplicationUser> userManager, //<----here
        ILogger<LoginModel> logger)
    {
        _signInManager = signInManager;
        _userManager = userManager;//<----here
        _logger = logger;
    }

并且在登录方法中,您可以检查 IsDeleted 属性,例如:

 var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
            if (result.Succeeded)
            {
                var user =  await _userManager.FindByNameAsync(Input.Email); //<----here
                if (user.IsDeleted)  //<----here
                {
                    ModelState.AddModelError(string.Empty, "This user doesn't exist.");

                    await _signInManager.SignOutAsync();

                    return Page();
                }


                _logger.LogInformation("User logged in.");
                return LocalRedirect(returnUrl);
            }

这是我的第一个堆栈溢出答案:)