问题
The standard way to check if the current request is authenticated is with HttpContext.User.Identity.IsAuthenticated
. I have just run into a case where the User
property is null so I cannot make the check!
I found an article describing the IIS request pipeline. I'm at the PostResolveRequestCache
stage which is clearly after AuthenticateRequest
! Why would HttpContext.User
still be null at that point?
回答1:
The FormsAuthenticationModule
was not running. I had to manually add it to my web.config
and User
was no longer null. I am investigating what caused the module not to run here.
回答2:
It depends on at what point of the request lifecycle you are checking this value. In your case, prefix your check for IsAuthenticated with a check for HttpContext.User == null. If NULL, not authenticated. :)
回答3:
The reason that HttpContext.User is null that no user authenticated jet. You may use Request.IsAuthenticated instead to check, and move to HttpContext.User only if it's true!
来源:https://stackoverflow.com/questions/23590706/why-is-httpcontext-user-null-after-authenticaterequest-is-complete