2016-11-17 174 views
3

我使用Adal和Azure Active Directory,我需要通過自定義OwinMiddleware添加額外聲明。 當我向該主體添加聲明時,我可以在當前請求中訪問它們。但是在頁面刷新之後,聲明就消失了。更新ClaimPrincipal中的聲明

我認爲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}); 

如何堅持到cookie中的新的權利要求任何想法?

回答

3

我已將索賠添加到錯誤的身份。他們必須被添加到身份變量而不是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});