2017-01-31 46 views
0

我在更新.NET .Net 4.6.2/MVC5中的ASP.Net Identity 2.2.1聲明時遇到問題。 更新索賠後,它通常會發送一個更新的cookie到瀏覽器,一切正常,但有時沒有設置cookie頭被髮送到瀏覽器。ASP.NET身份:在修改聲明後沒有更新cookie

我已經無法找出任何圖案,當它發生時,它失敗以外的其他的服務器發送在會議圓滿響應

Persistent-Auth: true 

HTTP標頭值。我不知道是什麼導致這個頭值被設置,它有時會出現在會話中,一旦它開始發送它,它將被髮送到會話的其餘部分,並嘗試更新聲明將永遠不會再爲該會話工作。

據我所見,我已經硬編碼isPersistent參數在每次調用ASP.Net身份時都是false,而且我看不到任何可能與此頭相關的其他內容。

我使用用於更新的權利要求的代碼是

public static void UpdateClaims(List<Claim> claims) 
{ 
    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; 
    var newIdentity = new ClaimsIdentity(HttpContext.Current.User.Identity); 

    foreach (Claim claim in claims) 
    { 
     Claim oldClaim = newIdentity.FindFirst(claim.Type); 
     if (oldClaim != null && oldClaim.Type != "") 
     { 
      newIdentity.RemoveClaim(oldClaim); 
     } 
     newIdentity.AddClaim(claim); 
    } 
    authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant 
      (new ClaimsPrincipal(newIdentity), new AuthenticationProperties { IsPersistent = false }); 
} 

這是正在從MVC動作方法調用。

有沒有人有什麼建議可能會出錯或甚至只是一個起點在哪裏看?我不知道是什麼導致了這個persistent-auth頭,但它看起來與問題有關;無論是問題的原因還是症狀,我都不知道。

我使用ASP.Net Identity 2.2.1與.Net 4.6.2。 我在Windows Server 2012R2上運行,並且問題似乎與IE11,Chrome和Firefox一起發生。 我正在使用Fiddler 4.6.3來查看http標題/響應。

更新: 我注意到,它似乎只在啓用Windows身份驗證時出錯。我的服務器有一個設置,允許用戶名/密碼,Windows身份驗證或兩者(用戶可以選擇使用用戶名/密碼作爲不同的用戶登錄)。當使用Windows身份驗證時,我最初使用Windows對用戶進行身份驗證,然後設置一個cookie,然後將該cookie用於會話中的所有未來請求。 如果禁用了Windows身份驗證,則更新這樣的聲明總是有效。如果啓用了Windows身份驗證,更新聲明通常的作品。

+0

我認爲你必須退出並登錄才能更新cookie中的聲明 – Saravanan

回答

1

我發現這個問題。它在嘗試更新索賠時使用了錯誤的身份。在我的場景中,有兩個身份對象,一個用於Windows身份驗證,另一個用於cookie身份驗證。在大多數情況下,HttpContext.Current.User.Identity獲取cookie身份驗證對象(這是具有聲明的身份驗證對象),但偶爾會向我提供Windows身份驗證對象,因此當我嘗試更新聲明時,它沒有執行任何操作。

問題是由與

ClaimsIdentity oldIdentity = principal.Identities.FirstOrDefault(i => i.AuthenticationType == "ApplicationCookie"); 
var newIdentity = new ClaimsIdentity(oldIdentity); 

更換

var newIdentity = new ClaimsIdentity(HttpContext.Current.User.Identity); 

解決它現在似乎無需退出/返回再次紮實工作。

我猜想012.http頭被髮送時,OWin認爲Windows身份驗證是主要身份,這就是爲什麼它的存在與無法更新索賠相關聯。

8

首先,你將兩個不同的東西混合在一起,雖然它是可以理解的,因爲它們的命名方式相似。 IsPeristent設置確定cookie是否是會話cookie或持久性cookie。換句話說:它決定了當瀏覽器關閉或在某個預定時間,cookie是否過期,無論瀏覽器是否關閉。

Persistent-Auth頭是一個優化頭,通知客戶端它不一定需要授權每個請求。它與IsPersistent標誌無關。

聲明在登錄時設置。期。如果您需要更新索賠,則必須簽署用戶並簽名。這可以通過編程方式完成(即無需用戶干預),但必須完成。換句話說,如果你需要改變一個要求,你需要的是改變在下一個請求可用,那麼你就跟着它具有:

身份2.0

AuthenticationManager.SignOut(); 
await SignInManager.SignInAsync(user); 

標識3.0

await SignInManager.RefreshSignInAsync(user); 
+0

我確實懷疑isPersistent標誌與http標題相關,但我仍然不知道爲什麼會出現http標題。 更改索賠來自其他地方的答案(例如http://stackoverflow.com/questions/24587414/how-to-update-a-claim-in-asp-net-identity),它似乎工作*大多數*的時間,但有時似乎卡住了。 – Mog0

3

而不是

authenticationManager.AuthenticationResponseGrant = 
    new AuthenticationResponseGrant(new ClaimsPrincipal(newIdentity), 
    new AuthenticationProperties { IsPersistent = false }); 

你應該使用

authenticationManager.SignIn(
    new AuthenticationProperties { IsPersistent = false }, 
    new ClaimsPrincipal(newIdentity));