【问题标题】:why after validation is false HttpContext.Current.Request.IsAuthenticated为什么验证后为假 HttpContext.Current.Request.IsAuthenticated
【发布时间】:2011-04-29 13:23:00
【问题描述】:

我已经使用表单身份验证在我的网站上创建了一个登录名, 我不明白为什么在创建票证并将其添加到 cookie 之后

如果我检查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>

谢谢。

【问题讨论】:

  • 五个问题,不接受。不好!!

标签: c# asp.net cookies forms-authentication


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-11-25
    • 2012-06-23
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 2016-06-18
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多