登录后有重复的声明?还是期间? [英] Duplicate Claims after logging in? Or during?

查看:36
本文介绍了登录后有重复的声明?还是期间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当我登录时,由于某种原因,正在设置重复的声明,如下图所示:

I noticed that when I am logging in, duplicate claims are being set for some reason like in this image:

我不确定导致此问题的原因是什么,但是因此,我无法将自定义声明添加到声明列表中.最奇怪的是,此代码通过Github来自另一台计算机,并且在那里工作.有清除饼干或其他东西的地方吗?

I'm not sure what is the cause of this issue but as a result, I am unable to add custom claims to the claims list. The strangest thing is that this code came from another computer via Github and it worked there. Is there an area to clear cookies or something?

这是我的登录代码

namespace Application.Areas.Identity.Pages.Account
{
    [AllowAnonymous]
    public class LoginModel : PageModel
    {
        private readonly SignInManager<ApplicationUsers> _signInManager;
        private readonly UserManager<ApplicationUsers> _userManager;
        private readonly ApplicationUsersData applicationUsersData;
        private readonly CustomClaimsCookieSignInHelper<ApplicationUsers> _customClaimsCookieSignInHelper;
        private readonly UserRolesData userRolesData;
        private readonly ILogger<LoginModel> _logger;
        private List<Claim> claims = new List<Claim>();
        [BindProperty]
        public LoginViewModel Input { get; set; }
        public string ReturnUrl { get; set; }
        [TempData]
        public string ErrorMessage { get; set; }

        public LoginModel(SignInManager<ApplicationUsers> signInManager, CustomClaimsCookieSignInHelper<ApplicationUsers> _customClaimsCookieSignInHelper, UserManager<ApplicationUsers> userManager, ApplicationUsersData applicationUsersData, UserRolesData userRolesData, ILogger<LoginModel> logger)
        {
            _signInManager = signInManager;
            _userManager = userManager;
            _logger = logger;
            this.applicationUsersData = applicationUsersData;
            this.userRolesData = userRolesData;
            this._customClaimsCookieSignInHelper = _customClaimsCookieSignInHelper;
        }

        public async Task OnGetAsync(string returnUrl = null)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                ModelState.AddModelError(string.Empty, ErrorMessage);
            }

            returnUrl = returnUrl ?? Url.Content("~/");

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
            ReturnUrl = returnUrl;
        }

        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
                if (result.Succeeded)
                {
                    var avm = applicationUsersData.GetByUsername(Input.Email);
                    var user = applicationUsersData.Get(avm.Id);
                    var roles = userRolesData.GetUserRoles(user.Id);
                    foreach (var item in roles)
                    {
                        var currentItem = new UserRoleDetailsViewModel
                        {
                            Id = item.Id,
                            Name = item.Name,
                            ApplicationId = item.ApplicationId,
                            ApplicationName = item.ApplicationName
                        };
                        var convertedItem = JsonConvert.SerializeObject(currentItem);
                        claims.Add(new Claim("Roles", convertedItem));
                    }
                    await _customClaimsCookieSignInHelper.SignInUserAsync(user, Input.RememberMe, claims);
                    _logger.LogInformation("User logged in.");
                    return LocalRedirect(returnUrl);
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return RedirectToPage("./Lockout");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return Page();
                }
            }
            // If we got this far, something failed, redisplay form
            return Page();
        }
    }
}

我的自定义声明Cookie帮助器:

My Custom Claims Cookie Helper:

public class CustomClaimsCookieSignInHelper<TIdentityUser> where TIdentityUser : IdentityUser
{

    private readonly SignInManager<TIdentityUser> _signInManager;

    public CustomClaimsCookieSignInHelper(SignInManager<TIdentityUser> signInManager)
    {
        _signInManager = signInManager;
    }

    public async Task SignInUserAsync(TIdentityUser user, bool isPersistent, IEnumerable<Claim> customClaims)
    {
        //var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
        //if (customClaims != null && claimsPrincipal?.Identity is ClaimsIdentity claimsIdentity)
        //{
        //    claimsIdentity.AddClaims(customClaims);
        //}
        //await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme,
        //    claimsPrincipal,
        //    new AuthenticationProperties { IsPersistent = isPersistent });
    }
}

如您所见,即使注释了此代码,在Cookie帮助程序通过后,声明仍然是重复的.

As you can see, even if this code is commented, after the cookie helper has been passed through, the claims are still duplicated.

推荐答案

我不确定这是哪里引起问题的,但是我自己的解决方案是清除所有现有的要求并重新添加它们

I'm not sure where this is causing issues but the solution I had to myself was to clear all the existing claims and just re-add them

var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
var identity = claimsPrincipal.Identity as ClaimsIdentity;
var claims = (from c in claimsPrincipal.Claims select c).ToList();
foreach(var item in claims)
{
    identity.RemoveClaim(item);
}
if (customClaims != null)
{
    identity.AddClaims(customClaims);
}

这篇关于登录后有重复的声明?还是期间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆