可更改的cookie到期时间

时间:2014-12-11 04:34:07

标签: c# cookies asp.net-web-api asp.net-identity owin

默认情况下,身份2.1身份验证Cookie到期时间通过Startup.Auth.csCookieAuthenticationOptions中全局设置。 有没有办法在运行中实现,具体取决于用户,因此它可以单独配置?

1 个答案:

答案 0 :(得分:2)

是的,有可能。使用AuthenticationProperties指定调用SignIn时的到期日期,如下所示。

var claims = new List<Claim>() { new Claim(ClaimTypes.Name, "Alice") };
var identity = new ClaimsIdentity(claims, "ApplicationCookie");

var properties = new AuthenticationProperties()
{
    ExpiresUtc = DateTimeOffset.Now.AddDays(1) // One day expiry for Alice
};

Request.GetOwinContext().Authentication.SignIn(properties, identity);