簡體   English   中英

ASP.NET身份:修改聲明后不更新cookie

[英]ASP.NET Identity: Not updating cookie after modifying claims

我在更新.Net 4.6.2 / MVC5中的ASP.Net Identity 2.2.1聲明時遇到問題。 更新聲明后,通常會將更新的Cookie發送到瀏覽器,並且一切正常,但有時沒有設置的Cookie標頭發送到瀏覽器。

除了失敗時,我無法識別任何模式,即服務器正在發送

Persistent-Auth: true

會話中每個響應的http標頭值。 我不知道是什么原因導致設置了此標頭值,並且有時它會出現在會話中,並且一旦開始發送它,它將在會話的其余部分發送,並且嘗試更新聲明將不再適用於該會話。

據我所知,在每次對ASP.Net身份的調用中,我都將isPersistent參數硬編碼為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操作方法調用的。

有沒有人有什么建議可能會出什么問題,甚至只是尋找起點? 我不知道是什么原因導致了持久身份驗證標頭,但它似乎與問題有關。 我不知道這是問題的原因還是症狀。

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

更新:我注意到只有啟用Windows身份驗證,它似乎才出錯。 我的服務器具有允許用戶名/密碼,Windows身份驗證或同時允許兩者的設置(用戶可以選擇使用用戶名/密碼以其他用戶身份登錄)。 當使用Windows auth時,我最初使用Windows對用戶進行身份驗證,然后設置一個cookie,然后將其用於會話中的所有將來請求。 如果禁用了Windows身份驗證,則始終可以像這樣更新聲明。 如果啟用了Windows身份驗證,則通常可以更新聲明。

首先,您將兩個不同的事物混為一談,盡管這是可以理解的,因為它們的名稱相似。 IsPeristent設置確定cookie是會話cookie還是持久cookie。 換句話說:它確定cookie是在瀏覽器關閉時還是在某個預定時間到期,以及瀏覽器是否關閉。

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

聲明是在登錄時設置的。 期。 如果您需要更新聲明,則必須注銷用戶並重新登錄。這可以通過編程方式完成(即,無需用戶干預),但必須完成。 換句話說,如果您需要更改索賠,並且您需要在下一個請求中可以使用該更改,那么請遵循以下步驟:

身份2.0

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

身份3.0

await SignInManager.RefreshSignInAsync(user);

代替

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

你應該使用

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

我發現了問題。 它嘗試更新聲明時使用了錯誤的身份。 在我的場景中,有兩個身份對象,一個用於Windows身份驗證,一個用於cookie身份驗證。 在大多數情況下, HttpContext.Current.User.Identity獲取cookie身份驗證對象(帶有聲明的對象),但偶爾會給我Windows身份驗證對象,因此當我嘗試更新該聲明時,它沒有做任何事情。

通過更換解決了問題

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

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

現在,它似乎可以正常運行,而無需再次注銷/重新登錄。

我猜想,當OWin認為Windows身份驗證是主要身份時,就會發送Persistent-Auth: true http標頭,因此這就是其存在與無法更新聲明相關的原因。

我認為在我們的案例中,這是ApplicationUserManager的錯誤/長期IoC生命周期/“作用域”,並且對於角色管理器也可能是相同的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM