Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么验证后HttpContext.Current.Request.IsAuthenticated为false_C#_Asp.net_Cookies_Forms Authentication - Fatal编程技术网

C# 为什么验证后HttpContext.Current.Request.IsAuthenticated为false

C# 为什么验证后HttpContext.Current.Request.IsAuthenticated为false,c#,asp.net,cookies,forms-authentication,C#,Asp.net,Cookies,Forms Authentication,我已经使用表单身份验证在我的网站上创建了登录, 我不明白为什么在创建票据并将其添加到cookies之后 如果我检查HttpContext.Current.Request.IsAuthenticated 我猜错了。只有在后续请求中,用户才能通过身份验证 这是我的密码 var fat = new FormsAuthenticationTicket( 1, username, DateTime.Now, DateTime.Now.AddMinutes(20),

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

如果我检查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,我会得到false,我认为在这一点上用户是经过身份验证的

这是我的配置

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


谢谢

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


成功验证用户后,您可以使用
Response.redirect
重定向到站点的已验证部分。另一种可能是直接使用该方法,它执行两件事:发出cookie并重定向到
标记中指定的页面。

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