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

时间:2018-11-19 17:02:52

标签: c# asp.net-core identity claims-based-identity

我正在尝试通过在数据库中添加逻辑“已删除”列来扩展身份用户。

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

我想在登录时检查“已删除”声明,并拒绝用户是否已删除其帐户。

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

我唯一能使它起作用的方法是覆盖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...

我的ApplicationUser类

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

最后是我的启动注册

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

谢谢

2 个答案:

答案 0 :(得分:3)

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

您在该方法中拥有的用户是Claimsprincipal,但未通过身份验证,在调用SignIn的代码之前以及在您的Claimsprincipal工厂方法被调用之前,auth中间件从请求中反序列化了该用户。

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

我认为最好以不同的方式拒绝用户,而不是在成功登录后拒绝用户,最好是在较低级别进行检查以使登录失败,而不是先成功然后退出。我在自定义用户存储区的项目中做到了这一点。

答案 1 :(得分:0)

在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;
    }

,并在Login方法中,您可以像下面那样检查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);
            }

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

相关问题