【问题标题】:FormsAuthentication not working. IsAuthenticated always return FalseFormsAuthentication 不起作用。 IsAuthenticated 总是返回 False
【发布时间】:2015-05-22 18:41:44
【问题描述】:

我正在使用 ASP.net 开发网站,我想实现基于表单的身份验证。 在 web.config 我使用这个

  <system.web>
    <authentication mode="Forms" >
      <forms loginUrl="login.aspx" protection="All" path="/" timeout="30" name=".ASPXFORMSDEMO">
      </forms>
    </authentication>
  </system.web>

在登录按钮点击我用这个。

FormsAuthenticationTicket ticket;
                string cookieString;
                HttpCookie cookie;

                ticket = new FormsAuthenticationTicket(1, txtiLoginEmail.Value, DateTime.Now, DateTime.Now.AddMinutes(5), chbRememberMe.Checked, null);
                cookieString = FormsAuthentication.Encrypt(ticket);
                cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieString);


                if (chbRememberMe.Checked)
                {
                    cookie.Expires = ticket.Expiration;
                    cookie.Path = FormsAuthentication.FormsCookiePath;
                }
                Response.Cookies.Add(cookie);

                string strRedirect = "post.aspx";
                Response.Redirect(strRedirect);

我有一个名为 post.aspx 的页面。我希望此页面仅供经过身份验证的用户访问。所以在 post.aspx 页面的页面加载事件中,我使用这段代码来检查它

  bool isAuthenticated = HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated;

但上面的代码总是返回 false。我在这里做错了什么?

【问题讨论】:

    标签: c# asp.net forms-authentication httpcontext httpcookie


    【解决方案1】:

    我想你忘了把:

    FormsAuthentication.SetAuthCookie(/*UserHere*/ ,false);
    

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 2021-01-24
      • 1970-01-01
      • 2019-10-25
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2019-01-05
      相关资源
      最近更新 更多