ASP.NET标识会话在prod服务器上过期

时间:2015-08-24 21:02:43

标签: c# asp.net session access-token expired-sessions

this tutorial应用到我的项目后,在我的本地主机上没有问题,但是当我部署到plesk上的prod服务器时,会话过期太快。可能不到5分钟,而且一直都是这样。

由于教程使用localStorage而不是cookie,我无法确定是否应该检查cookie超时。

哦另一方面我在Startup.Auth.cs中有这个:

    public void ConfigureAuth(IAppBuilder app)
    {
        //...
        OAuthOptions = new OAuthAuthorizationServerOptions
        { 
            //...
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
        };
    }

prod IIS如何设置超时,我应该在web.config中做什么来覆盖这个超时?

1 个答案:

答案 0 :(得分:2)

我认为你的意思是14天对你的生产环境来说还不够长?如果不是,你将不得不通过“太快”澄清你究竟是什么意思。

最简单的方法是在web.config中添加一个设置。

<appSettings>
    <add key="cookieExpirationDays" value="30"/>
</appSettings>

然后在你的方法中设置它。

 public void ConfigureAuth(IAppBuilder app) {
    //...
   var daysStr = System.Configuration.ConfigurationManager.AppSettings["cookieExpirationDays"];
   var days = string.IsNullOrEmpty(daysStr) ? 14 : int.Parse(daysStr);
    OAuthOptions = new OAuthAuthorizationServerOptions
    { 
    //...
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(days),
  };
}

编辑(响应OP编辑)

如果您使用cookie来持久保存身份验证令牌,请尝试此操作(请参阅初始化程序中的最后一行)。

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    CookieName = "SecurityCookie",
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Authentication/Login"),
    CookieSecure = CookieSecureOption.SameAsRequest,
    CookieHttpOnly = true,
    AuthenticationMode = AuthenticationMode.Active,
    Provider = cookieProvider, // instance of Microsoft.Owin.Security.Cookies.CookieAuthenticationProvider
    LogoutPath = new PathString("/Authentication/LogOff"),
    SlidingExpiration = true,
    ExpireTimeSpan = TimeSpan.FromDays(days),
});

修改2 添加了示例链接

Complete tutorial

General Microsoft Documentation and Help