問(wèn)題描述
我將 Adal 與 Azure Active Directory 一起使用,我需要通過(guò)自定義 OwinMiddleware 添加額外的聲明.當(dāng)我向該主體添加聲明時(shí),我可以在當(dāng)前請(qǐng)求中訪問(wèn)它們.但刷新頁(yè)面后,聲明消失了.
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 current request. But after a page refresh, the claim is gone.
我以為 Owin 處理了聲明的序列化并將其放入 cookie 本身,但事實(shí)并非如此.
I thought Owin handled serialization of claims and put it into a cookie itself, but this doesn't seem to be the case.
我添加聲明如下:
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});
關(guān)于如何將新的聲明持久化到 cookie 上有什么想法嗎?
Any ideas on how to persist the new claims to the cookie?
推薦答案
我已將聲明添加到錯(cuò)誤的身份.必須將它們添加到身份變量而不是 claimIdentity.
I've added the claims to the wrong Identity. They had to be added to the identity variable instead of the claimsIdentity.
工作代碼:
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});
這篇關(guān)于更新 ClaimsPrincipal 中的聲明的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!