Why is HttpContext.User null after AuthenticateRequest is complete

 ̄綄美尐妖づ 提交于 2019-12-25 02:29:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!