ASP.NET标识:修改声明后不更新cookie

Mog*_*og0 0 c# asp.net asp.net-mvc asp.net-identity

我在.Net 4.6.2/MVC5中更新ASP.Net Identity 2.2.1的声明时遇到问题.更新声明后,它通常会向浏览器发送更新的cookie,一切正常,但有时没有设置cookie标头发送到浏览器.

我无法确定何时发生任何模式,而不是在发生故障时,服务器正在发送

Persistent-Auth: true
Run Code Online (Sandbox Code Playgroud)

会话中每个响应的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 });
}
Run Code Online (Sandbox Code Playgroud)

这是从MVC动作方法调用的.

有没有人有什么建议可能会出问题,甚至只是一个起点在哪里看?我不知道是什么导致持久性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 auth时,我最初使用Windows对用户进行身份验证,然后设置一个cookie,然后我将其用于会话中的所有未来请求.如果禁用了Windows身份验证,则更新此类声明始终有效.如果启用了Windows身份验证,则更新声明通常有效.

Chr*_*att 14

首先,你将两种不同的东西混为一谈,虽然这是可以理解的,因为它们的名字相似.该IsPeristent设置确定cookie是会话cookie还是持久cookie.换句话说:它确定当浏览器关闭时或者在某个预定时间,无论浏览器是否关闭,cookie是否会过期.

所述Persistent-Auth报头是一种优化报头通知客户机,它不一定需要授权每个请求.它与IsPersistent国旗无关.

索赔是在登录时设置的.期.如果您需要更新声明,则必须将用户签名并重新签名.这可以通过编程方式完成(即无需用户干预),但必须完成.换句话说,如果您需要更改声明,并且您需要在下一个请求中提供该更改,那么您可以使用以下命令:

身份2.0

AuthenticationManager.SignOut();
await SignInManager.SignInAsync(user);
Run Code Online (Sandbox Code Playgroud)

身份3.0

await SignInManager.RefreshSignInAsync(user);
Run Code Online (Sandbox Code Playgroud)


小智 5

代替

authenticationManager.AuthenticationResponseGrant = 
    new AuthenticationResponseGrant(new ClaimsPrincipal(newIdentity), 
    new AuthenticationProperties { IsPersistent = false });
Run Code Online (Sandbox Code Playgroud)

你应该使用

authenticationManager.SignIn(
    new AuthenticationProperties { IsPersistent = false }, 
    new ClaimsPrincipal(newIdentity));
Run Code Online (Sandbox Code Playgroud)