2010-10-23 90 views
2

我创建使用窗体身份验证, 我的网站登录,我不明白为什么创造了机票,将它添加到饼干后为什么后验证是假HttpContext.Current.Request.IsAuthenticated

如果我检查HttpContext.Current.Request.IsAuthenticated

我得到错误。只有在连续请求的用户成为身份验证

这是我的代码

var fat = new FormsAuthenticationTicket(
    1, 
    username, 
    DateTime.Now, 
    DateTime.Now.AddMinutes(20), 
    rememberMe, 
    contact.Id + "," + contact.Role.Id, 
    FormsAuthentication.FormsCookiePath); 
HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat))); 

在这一点上,如果我检查HttpContext.Current.Request.IsAuthenticated我得到假的,我想到了这点用户认证...

这是我的配置

<authentication mode="Forms"> 
    <forms loginUrl="/Admin/Login.aspx" name="FormAuthentication" /> 
</authentication> 
<authorization> 
    <deny users="?"/> 
</authorization> 

感谢。

+0

五个问题,不接受。不太好!! – 2011-04-11 06:38:17

回答

5

因为这就是它的工作原理。该属性尝试从请求中读取cookie,但没有任何因为请求发送时客户端尚未通过身份验证。 cookie已设置,并在连续的客户端请求发送到服务器。

成功验证用户身份后,您可以使用Response.Redirect重定向到网站的身份验证部分。另一种可能性是直接使用RedirectFromLoginPage方法执行两件事:它发出cookie并重定向到<forms>标记中指定的页面。

相关问题