Update claims in ClaimsPrincipal

后端 未结 1 813
我寻月下人不归
我寻月下人不归 2021-01-04 23:45

I am using Adal with Azure Active Directory and I need to add extra claims via custom OwinMiddleware. When I add claims to this principal, I am able to access them in the cu

1条回答
  •  天涯浪人
    2021-01-05 00:21

    I've added the claims to the wrong Identity. They had to be added to the identity variable instead of the claimsIdentity.

    Working code:

            var claimsIdentity = (ClaimsIdentity) context.Authentication.User.Identity;
            if (!claimsIdentity.IsAuthenticated) return;
    
            var identity = new ClaimsIdentity(claimsIdentity);
    
            var currentTenantClaim = GetTenantClaim(identity);
    
            if (currentTenantClaim != null)
                identity.RemoveClaim(currentTenantClaim);
    
            identity.AddClaim(new Claim(ClaimTypes.CurrentTenantId, id));
    
            context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant
                (new ClaimsPrincipal(identity), new AuthenticationProperties {IsPersistent = true});
    

    0 讨论(0)
提交回复
热议问题