简体   繁体   English

安装.NET Framework 4.5后,HttpContext.Current.User为null

[英]HttpContext.Current.User is null after installing .NET Framework 4.5

I was using .NET 4.0 VS10. 我使用的是.NET 4.0 VS10。 I had working WCF services that were using forms authentication. 我有使用表单身份验证的WCF服务。 I upgraded to VS11, .NET 4.5. 我升级到VS11,.NET 4.5。

Now my HttpContext.Current.User is null, in a wcf request. 现在我的HttpContext.Current.User在wcf请求中为null。 I injected a cookie from login to this request. 我从登录中注入了一个cookie给这个请求。

HttpRequestMessageProperty httpRequest;
...
httpRequest.Headers.Add(HttpRequestHeader.Cookie, cookie);

Any ideas how to make it work again? 任何想法如何让它再次工作?

Already have the 已经有了

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

and the

[AspNetCompatibilityRequirements(RequirementsMode = spNetCompatibilityRequirementsMode.Required)]

If you are running into this issue in an Oneway (operations marked with [OperationContract(IsOneWay=true)] ) WCF operation contract, here is the reason for the issue. 如果您在Oneway中遇到此问题(标记为[OperationContract(IsOneWay = true)]的操作)WCF操作合同,这就是问题的原因。 HttpContext is an asp.net construct. HttpContext是一个asp.net构造。 After a one way operation is successfully invoked the service model sends back a 202 (Accepted) reply back to the client. 在成功调用单向操作之后,服务模型将202(已接受)回复发送回客户端。

  • In 4.5 asp.net clears away the HttpContext as the response has been sent back to the client already which is by design. 在4.5中,asp.net清除了HttpContext,因为响应已经被发送回客户端,这是设计的。
  • In 4.0 this is just a happy coincidence that this unsupported scenario worked. 在4.0中,这只是一个幸福的巧合,这个不受支持的场景有效。

Since we found a few customer reports on this, a quirked fix is made in .net 4.5.1 update. 由于我们在此发现了一些客户报告,因此在.net 4.5.1更新中进行了一个怪异的修复。 To give more details about the fix: 要提供有关修复的更多详细信息:

  1. If your project is still targeting 4.0 & you are running your application on a 4.5.1 machine then your issue will be fixed (Internally checks for the target framework version). 如果您的项目仍然以4.0为目标并且您在4.5.1计算机上运行应用程序,那么您的问题将得到解决(内部检查目标框架版本)。
  2. If your project is retargeted to 4.5 then Httpcontext.Current.User will again become null for the reasons I mentioned above. 如果您的项目重新定位到4.5,那么Httpcontext.Current.User将再次变为null,原因如上所述。 Try using OperationContext.Current.ServiceSecurityContext.WindowsIdentity to get the user identity instead of HttpContext.Current.User property. 尝试使用OperationContext.Current.ServiceSecurityContext.WindowsIdentity来获取用户身份而不是HttpContext.Current.User属性。 This property is host independent and gets populated when your client is authenticated. 此属性与主机无关,并在客户端通过身份验证时填充。

There's a solution to a similar problem here : 还有一个类似的问题的解决方案在这里

Add

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

to your config file. 到您的配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IsOneWay = true时HttpContext.Current.User为null - HttpContext.Current.User is null when IsOneWay = true 设置HttpContext.Current.User引发null引用异常 - setting HttpContext.Current.User throws null reference exception HttpContext.Current.User对于无状态会话是否安全? - Is HttpContext.Current.User Safe for Stateless Session? HttpContext.Current.User和HttpContext.User有什么区别 - What is the difference between HttpContext.Current.User and HttpContext.User 是(HttpContext.Current.User!= null)足以假设FormsAuthentication已对用户进行了身份验证 - Is (HttpContext.Current.User != null) enough to assume that FormsAuthentication has authenticated the user ASP.Net Httpcontext.Current.User显示生成线程的最后一个用户 - ASP.Net Httpcontext.Current.User shows last user who spawned thread ASP.Net MVC 5-在控制器外部访问HttpContext.Current.User的正确方法? - ASP.Net MVC 5 - Correct way to access HttpContext.Current.User outside of controller? 如何在 MVC 中的 Application_PostAuthenticateRequest 之后引用 HttpContext.Current.User? - How do I reference the HttpContext.Current.User after the Application_PostAuthenticateRequest in MVC? HttpContext.Current.User 和 this.User.Identity 的区别 - Difference between HttpContext.Current.User and this.User.Identity HttpContext.Current.User未填充启用Windows身份验证 - HttpContext.Current.User not populated with Windows Authentication enabled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM