【发布时间】: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