【问题标题】:Changeable cookie expiry time可更改的 cookie 过期时间
【发布时间】:2015-02-09 11:57:40
【问题描述】:

默认情况下,Identity 2.1 身份验证 cookie 的到期时间在 Startup.Auth.cs 中通过 CookieAuthenticationOptions 全局设置。 有没有一种方法可以即时执行此操作,具体取决于用户,因此可以单独配置?

【问题讨论】:

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


    【解决方案1】:

    是的,这是可能的。使用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);
    

    【讨论】:

    • 谢谢,这就是我想要的。然而,新的身份SignInManager 存在一个问题:覆盖AuthenticationProperties 需要覆盖SignInAsync,这是不可插入的。所以需要在每个新版本中验证覆盖!
    猜你喜欢
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 2011-03-01
    • 1970-01-01
    • 2012-03-06
    • 2013-07-06
    相关资源
    最近更新 更多