HttpContext.Current.User is null after installing .NET Framework 4.5

故障原因:从framework4.0到framework4.5的升级过程中,原有的form认证方式发生了变化,所以不再支持User.Identity.Name原有存储模式(基于cookie),要恢复这个功能,我也是偶然间发现的。

一下五种解决方案根据具体情况供参考

1、降级处理,把你的framework降到4.0

<compilation debug="true " targetFramework=" 4.0" />

< httpRuntime targetFramework ="4.0" />

2、set the Current User myself in the Global.asax:(不用降级只需加上这段代码)

 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]);
    }
}

3、 <machineKey validationKey="12..." decryptionKey="34..." 
                validation="SHA1" decryption="AES"
                compatibilityMode="Framework20SP1" />

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.)

HttpContext.Current.User is null after installing .NET Framework 4.5

时间: 2024-08-08 10:52:05

HttpContext.Current.User is null after installing .NET Framework 4.5的相关文章

System.Web.HttpContext.Current.Session为NULL值的问题?

自定义 HTTP 处理程序,从IHttpHandler继承,在写System.Web.HttpContext.Current.Session["Value"]的时 候,没有问题,但想将这个Session写到某个变量时或判断是否为空时 如:HttpContext.Current.Session["Value"]==null,发现Session的值为NULL,后来查MSDN,看到 “在自定义 HTTP 处理程序中实现 IRequiresSessionState 接口,以

异步 HttpContext.Current 为空null 另一种解决方法

1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Current为null的情况,如下: 2.网络解答 从百度与谷歌查询,分以下两种情况进行解答: 1.更改web.config配置文件 Stackoverflow给出如下解决方案:http://stackoverflow.com/questions/18383923/why-is-httpcontext-cur

WCF 中HttpContext.Current为null的解决办法

解决WCF hosting IIS的环境中使HttpContext.Current不为NULL, 来我们传递每次请求中的Cookie 经过测试发现只要注意下面亮点就可以了: 1. 在hosting WCF的web.config中加入: <system.serviceModel>   <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/></system.serviceModel>

异步任务,HttpContext.Current为null解决办法

最近在开发一个后台管理系统项目,为了提高登录的速度,就把记录登录日志放到一个异步任务里面. Action taskAction = () => { SaveLog(); }; Task task = new Task(taskAction); task.Start(); 有一段时间日志记录是正常的,突然有一天就不work了,本地调试也能重现这个错误,是System.Web.HttpContext.Current为null造成的,获取不了当前的用户名. 在网上搜了一下,确实很多用户遇到了,有推荐缓

HttpContext.Current并非无处不在

原文地址:http://www.cnblogs.com/fish-li/archive/2013/04/06/3002940.html 阅读目录 开始 无处不在的HttpContext HttpContext.Current到底保存在哪里? HttpContext并非无处不在! 如何获取文件绝对路径? 异步调用中如何访问HttpContext? 安全地使用HttpContext.Current 了解ASP.NET的开发人员都知道它有个非常强大的对象 HttpContext,而且为了方便,ASP.

Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,这两种方式有什么区别呢?我们先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前

HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cache.       我们再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现: HttpContext.Cache和HttpRuntime.Cache实现    //System.Web.HttpContext.Cache属性实现   

HttpContext.Current

阅读目录 开始 无处不在的HttpContext HttpContext.Current到底保存在哪里? HttpContext并非无处不在! 如何获取文件绝对路径? 异步调用中如何访问HttpContext? 安全地使用HttpContext.Current 了解ASP.NET的开发人员都知道它有个非常强大的对象 HttpContext,而且为了方便,ASP.NET还为它提供了一个静态属性HttpContext.Current来访问它, 今天的博客打算就从HttpContext.Current

HttpContext.Current.Server.MapPath(&quot;/&quot;) 未将对象设置到对象的实例异常。

多线程中的System.Web.HttpContext.Current.Server.MapPath("/") 多线程中Server.MapPath会失效... 网上找到几种解决方法,现在整理如下: 第一种: System.Web.HttpContext.Current.Server.MapPath("/")  这个常用来表示网站的根目录,但是在多线程中,会发生未将对象引用设置到对象的实例. 所以不要分布在不同的类中,尽量在一个全局位置,然后其它类共用这个,毕竟网站