HttpContext.Current.User is null after installing .NET Framework 4.5
2014-07-16 10:21
609 查看
故障原因:从framework4.0到framework4.5的升级过程中,原有的form认证方式发生了变化,所以不再支持User.Identity.Name原有存储模式(基于cookie),要恢复这个功能,我也是偶然间发现的。
一下五种解决方案根据具体情况供参考
1、降级处理,把你的framework降到4.0
[align=left]<compilation debug="true " targetFramework=" 4.0" />[/align]
[align=left] < httpRuntime targetFramework ="4.0" />[/align]
[align=left] [/align]
[align=left]2、set the Current User myself in the Global.asax:(不用降级只需加上这段代码)[/align]
[align=left] 3、 <machineKey validationKey="12..." decryptionKey="34..."
validation="SHA1" decryption="AES"
compatibilityMode="Framework20SP1" />[/align]
4、SessionAuthenticationModule
Since you're in 4.5 and since you're manually issuing the cookie and since you're already intercepting the cookie, then you're not really using the forms auth HTTP module at all. I'd remove that for now. Also, change your module to handle AuthenticateRequest. See how that works.
Then once you do get it working, I'd scrap it all in favor of claims and using the SessionAuthenticationModule which is new and built into 4.5. It is far simpler and will serialize all of your roles into a cookie for you.
http://msdn.microsoft.com/zh-cn/library/system.identitymodel.services.sessionauthenticationmodule(v=vs.110).aspx
5
Make sure that the machineKey compatibility mode is the same between all applications:
<machineKey compatibilityMode="Framework20SP1" />
(The above is the default for 2.0 / 4.0 applications but is not the default for 4.5 applications, so it will have to be set explictly in the 4.5 application.)
一下五种解决方案根据具体情况供参考
1、降级处理,把你的framework降到4.0
[align=left]<compilation debug="true " targetFramework=" 4.0" />[/align]
[align=left] < httpRuntime targetFramework ="4.0" />[/align]
[align=left] [/align]
[align=left]2、set the Current User myself in the Global.asax:(不用降级只需加上这段代码)[/align]
private void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie != null) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(ticket), new string[0]); } }
[align=left] 3、 <machineKey validationKey="12..." decryptionKey="34..."
validation="SHA1" decryption="AES"
compatibilityMode="Framework20SP1" />[/align]
4、SessionAuthenticationModule
Since you're in 4.5 and since you're manually issuing the cookie and since you're already intercepting the cookie, then you're not really using the forms auth HTTP module at all. I'd remove that for now. Also, change your module to handle AuthenticateRequest. See how that works.
Then once you do get it working, I'd scrap it all in favor of claims and using the SessionAuthenticationModule which is new and built into 4.5. It is far simpler and will serialize all of your roles into a cookie for you.
http://msdn.microsoft.com/zh-cn/library/system.identitymodel.services.sessionauthenticationmodule(v=vs.110).aspx
5
Make sure that the machineKey compatibility mode is the same between all applications:
<machineKey compatibilityMode="Framework20SP1" />
(The above is the default for 2.0 / 4.0 applications but is not the default for 4.5 applications, so it will have to be set explictly in the 4.5 application.)
相关文章推荐
- Why is HttpContext.Current null after await?
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- 在IIS7下使用HttpModule的过程中遇到很诡异的问题,HttpContext.Current.User为NULL
- HttpContext.Current.User.Identity.IsAuthenticated 这个是表示的什么
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏
- 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
- ASP.NET HttpContext.Current在新建线程中会返回null
- ASP.NET Identity V2在多线程中UserManager获取的httpContext.Current始终为null的解决办法
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 350人阅读 评论(0) 收藏
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- 为什么取不到HttpContext.Current.User.Identity.Name值?郁闷
- ASP.NET 使用HttpContext.Current.Cache
- 多线程中使用HttpContext.Current为null的解决办法
- HttpContext.Current.Handler asp.net 获取前当前页的所有表单元素
- HttpContext.Current.Session为null的问题
- WCF Service中HttpContext.Current为null的解决办法
- asp.net页面传递参数的另一种用法 HttpContext.Current.Items