Windows身份验证(集成管道)下的HttpContext.Current.User为null

时间:2011-12-23 19:08:25

标签: asp.net asp.net-mvc iis-express

在web.config中:

<configuration>
    <authentication mode="Windows">
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
</configuration>

在global.asax

protected void Application_AuthenticateRequest(object sender, EventArgs args)
{
    tracker.LogRequest(HttpContext.Current.User, DateTime.Now)
    ///THIS IS ALWAYS NULL!!!
}

我对此感到困惑,任何想法?

1 个答案:

答案 0 :(得分:3)

我认为这是听错事件的症状。你应该听Application.PostAuthenticateRequest

使用我对我的本地域的Active Directory进行身份验证的项目运行一段示例代码,询问User对象是否为空:

<强>代码

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
    Debug.WriteLine("Authenticate Request: " & (HttpContext.Current.User Is Nothing))
End Sub

Sub Application_PostAuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
    Debug.WriteLine("Post-authenticate Request: " & (HttpContext.Current.User Is Nothing))
End Sub

<强>输出

  

验证请求:是

     

验证后验证请求:错误

PostAuthenticateRequest事件之后,HttpContext.Current.User.Identity属性是System.Security.Principal.GenericIdentity的实例,用于未经身份验证的请求。