HttpContext.Current.User.Identity.IsAuthenticated返回false

Stu*_*art 4 c# asp.net authentication

我有一个奇怪的问题.

我有一个包含以下代码的页面.

if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        Server.Transfer(@"~/Views/Public/Unauthorised.aspx");
        return;
    }
Run Code Online (Sandbox Code Playgroud)

出于某种原因,对于一个用户(我们将其缩小到他的单个机器和Windows登录配置文件),IsAuthenticated总是返回false.即使他已登录网站,也可以导航到需要经过身份验证的用户的其他页面.除了这一页.

我们检查了机器是否接受了所有cookie,我们仍然遇到同样的问题.我不知道从哪里开始......有什么建议吗?

Ari*_*tos 5

至少有两种已知的案例可以解决这种问题.

第一种情况是您已requireSSL="true"启用身份验证会话web.config并从非安全页面调用该功能.因此,如果您使用,请仔细检查页面是否安全requireSSL="true"

Debug.Assert(Request.IsSecureConnection, "The IsAuthenticated will fail.");
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
    Server.Transfer(@"~/Views/Public/Unauthorised.aspx");
    return;
}
Run Code Online (Sandbox Code Playgroud)

第二种情况,当您没有domain="site.com"在web.config中再次设置身份验证会话时,您尝试从一次www.yoursitename.com和另一次请求cookie yoursitename.com.在这种情况下,身份验证cookie是不同的,它将失败.所以在web.config上设置该参数.

<authentication mode="Forms">
  <forms domain="yoursitename.com"  />
</authentication>
Run Code Online (Sandbox Code Playgroud)