Ide*_*ity 5 c# azure-active-directory adal
我正在使用Adal和Azure Active Directory,我需要通过自定义OwinMiddleware添加额外的声明.当我向此主体添加声明时,我可以在当前请求中访问它们.但是在页面刷新之后,声明就消失了.
我认为Owin处理了声明的序列化并将其放入cookie本身,但事实并非如此.
我将声明添加如下:
var claimsIdentity = (ClaimsIdentity) ClaimsPrincipal.Current.Identity;
if (!claimsIdentity.IsAuthenticated) return;
var identity = new ClaimsIdentity(claimsIdentity);
var currentTenantClaim = GetTenantClaim();
if (currentTenantClaim != null)
claimsIdentity.RemoveClaim(currentTenantClaim);
claimsIdentity.AddClaim(new Claim(ClaimTypes.CurrentTenantId, id));
context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant
(new ClaimsPrincipal(identity), new AuthenticationProperties {IsPersistent = true});
Run Code Online (Sandbox Code Playgroud)
关于如何将新声明保留在cookie中的任何想法?
Ide*_*ity 10
我已将声明添加到错误的标识中.必须将它们添加到identity变量而不是claimIdentity.
工作代码:
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});
Run Code Online (Sandbox Code Playgroud)
小智 5
这对我有用,使用 .NET 6:
var identity = (ClaimsIdentity)Request.HttpContext.User.Identity;
identity.AddClaim(new Claim("ClaimName", "ClaimValue"));
Run Code Online (Sandbox Code Playgroud)
如果我想以这个身份检查这个声明,这就是我所做的
var Claims = User.Claims;
Run Code Online (Sandbox Code Playgroud)
希望它可以帮助某人
| 归档时间: |
|
| 查看次数: |
8545 次 |
| 最近记录: |