C# asp.net 网页中的 Cookie 过期过快

Cookies expiring too quickly enough in c# asp.net webpages

出于某种原因,我的 cookie 没有像应有的那样持续很长时间,通常在 15 分钟内用户必须重新登录。我正在使用网络表单登录,当用户登录时,我会像这样设置 cookie。

   HttpCookie cookie = new HttpCookie("UserName", "Joe");
   cookie.Expires = DateTime.UtcNow.AddDays(5);    
   HttpContext.Current.Response.SetCookie(cookie);

当我读取 cookie 时,我就是这样做的

if (Request.Cookies["UserName"] != null && Request.Cookies["UserName"].value == "Joe")

这是我的 web.config 文件中用于身份验证的内容,每个人都会在不到 1 小时内被弹回 loginpage.aspx

<authentication mode="Forms">
  <forms name="LoginPage" path="/" loginUrl="/LoginPage.aspx" defaultUrl="Index.aspx"/>
</authentication>
<authorization>
  <deny users="?"/>
  <!-- Deny all anonymous users-->
  <allow users="*"/>
  <!-- Allow all authanticated users-->
</authorization>

我认为这是超时工作。要修复该问题,请将属性 timeout="0" 添加到您的元素。 MSDN