表单身份验证cookie未过期

stt*_*taq 3 asp.net-mvc forms-authentication asp.net-mvc-4

我正在尝试为MVC站点实现一个非常基本的Asp.net表单身份验证机制.我得到的问题是我的身份验证cookie设置为在一年后过期,而我不希望它在这么长时间后过期.这是我的一些代码:

web.config中

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

调节器

...
FormsAuthentication.SetAuthCookie(username, false);
...
Run Code Online (Sandbox Code Playgroud)

我找到了这个答案(这个问题类似,但在我的情况下,超时永远不会发生),但这是使cookie过期或我在这里做错了什么的唯一方法吗?

当我查看cookie时,它会在一年后过期,即使它会在几分钟后过期,为什么呢?

我想要的是某种程度上用户在一段时间后退出并且我认为在forms标签中设置过期会完成这项工作吗?

stt*_*taq 8

在找到解决方案后,将近一个月,100次观看,没有答案.

首先,web.config仅当cookie被设置为持久性时才在工作中指定超时,即持久cookie也可以到期.最初我错误地认为持久cookie不能过期.事实上,如果我总是将cookie设置为持久性,那么我原来的代码就可以了.

其次,我认为成员资格提供者无需按照上述评论中的建议进行表单身份验证.

以下是我现在创建身份验证cookie的方法:

HttpCookie authCookie = FormsAuthentication.GetAuthCookie(username, isPersistent);
if (!isPersistent)
{
    //this is because if it was not set then it got 
    //automatically set to expire next year even if 
    //the cookie was not set as persistent
    authCookie.Expires = DateTime.Now.AddMinutes(15);
}

Response.Cookies.Add(authCookie); 
Run Code Online (Sandbox Code Playgroud)

如果有替代品,请告诉我?