在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!!!
}
我对此感到困惑,任何想法?
答案 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
的实例,用于未经身份验证的请求。