验证后为什么是假HttpContext.Current.Request.IsAuthenticated

Gig*_*apr 2 c# asp.net cookies forms-authentication

我使用表单身份验证在我的网站上创建了一个登录,我不明白为什么在创建故障单并将其添加到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)));
Run Code Online (Sandbox Code Playgroud)

在这一点,如果我检查HttpContext.Current.Request.IsAuthenticated我得到假,我认为这一点用户被认证...

这是我的配置

<authentication mode="Forms">
    <forms loginUrl="/Admin/Login.aspx" name="FormAuthentication" />
</authentication>
<authorization>
    <deny users="?"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Dar*_*rov 5

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

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