【问题标题】:Remember me functionality in ASP.NET Form Authentication doesn't work记住我在 ASP.NET 表单身份验证中的功能不起作用
【发布时间】:2011-06-18 13:54:58
【问题描述】:

我正在使用 ASP.NET 表单身份验证将用户登录到我们正在开发的网站。

部分功能是“记住我”复选框,如果用户选中它,它会记住用户一个月。

用户登录代码如下:

public static void Login(HttpResponse response, string username,
  bool rememberMeChecked)
{
  FormsAuthentication.Initialize();
  FormsAuthenticationTicket tkt = new FormsAuthenticationTicket(1, username, DateTime.Now,
    DateTime.Now.AddMinutes(30), rememberMeChecked,
    FormsAuthentication.FormsCookiePath);
  HttpCookie ck = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(tkt));
  ck.Path = FormsAuthentication.FormsCookiePath;

  if (rememberMe)
    ck.Expires = DateTime.Now.AddMonths(1);

  response.Cookies.Add(ck);
}

web.config 中的相关部分是这样的:

<authentication mode="Forms">
  <forms loginUrl="Home.aspx" defaultUrl="~/" slidingExpiration="true" timeout="43200" />
</authentication>

这会很好地记录用户,但如果他们不使用该站点,则会在半小时后将其注销,尽管其持久性属性 (rememberMeChecked) 设置为 true,如果为 true,则 cookie 设置为在月。我这里有什么遗漏吗?

提前致谢, F

【问题讨论】:

  • 我不确定这是否会对这种情况产生影响,但是,使用FormsAuthentication.RedirectFromLoginPage(userName, rememberMe) 有什么问题?是否需要手动创建票证?如果您在配置中指定超时,那么您不需要在代码中手工制作它,AFAIK。另外,rememberMe 设置在哪里?

标签: c# forms-authentication remember-me


【解决方案1】:

尝试在 web.config 中设置 forms 标签的 Name 属性

另外,Firecookie 非常擅长调试这类问题

再次阅读您的代码,您还在票证构造函数中指定了DateTime.Now.AddMinutes(30)...必须检查它是否会影响它

【讨论】:

  • name 链接已损坏。
【解决方案2】:

我意识到必须更新身份验证票,而不仅仅是读取。我的 Application_BeginRequest 方法现在看起来像这样:

 if (!Request.IsAuthenticated)
  {
    HttpCookie ck = Request.Cookies[FormsAuthentication.FormsCookieName];

    if (ck != null)
    {
      FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(ck.Value);
      UserManagementUtils.RenewFormsAuthenticationTicket(Response, oldTicket);
    }
  }

这似乎已经解决了。

【讨论】:

  • 这应该由框架来处理。我认为上面答案中提到的问题是您没有设置票证的到期时间只是cookie。
【解决方案3】:

您的身份验证票证似乎仍配置为半小时后过期,即使 cookie 本身在 30 天后过期也是如此。您可能还需要延长票证的使用寿命:

public static void Login(HttpResponse response, string username,
    bool rememberMeChecked)
{
    DateTime expiration = DateTime.Now.AddMinutes(30);
    if (rememberMe) {
        expiration = DateTime.Now.AddMonths(1);
    }

    FormsAuthentication.Initialize();
    FormsAuthenticationTicket tkt = new FormsAuthenticationTicket(1, username,
        DateTime.Now, expiration, rememberMeChecked,
        FormsAuthentication.FormsCookiePath);

    HttpCookie ck = new HttpCookie(FormsAuthentication.FormsCookieName,
        FormsAuthentication.Encrypt(tkt));
    ck.Path = FormsAuthentication.FormsCookiePath;
    response.Cookies.Add(ck);
}

【讨论】:

    【解决方案4】:

    您在FormsAuthenticationTicket 的构造函数中指定了DateTime.Now.AddMinutes(30)。这就是设置登录的过期时间。

    【讨论】:

      【解决方案5】:

      在我看来,您检查的是“rememberMe”,而不是您传递的名为“rememberMeChecked”的变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-11
        • 2010-10-05
        • 1970-01-01
        • 2015-01-19
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        相关资源
        最近更新 更多