开发者

How to access HttpContext.Current.User.Username in WCF service

开发者 https://www.devze.com 2023-03-21 15:08 出处:网络
How can I access HttpContext.Current.User.Username from a web appl开发者_开发问答ication in a WCF service?Generally you don\'t - HttpContext is an ASP.NET concept and doesn\'t apply to WCF unless you

How can I access HttpContext.Current.User.Username from a web appl开发者_开发问答ication in a WCF service?


Generally you don't - HttpContext is an ASP.NET concept and doesn't apply to WCF unless you run it with ASP.NET Compatibility turned on.

If you want the current user in WCF then use ServiceSecurityContext.Current.PrimaryIdentity or get the security context via the OperationContext.


Actually, with Asp.Net Compatibility mode on, you can access HttpContext.Current.User from a WCF service hosted in the site. See Microsoft's site for details: https://msdn.microsoft.com/en-us/library/aa702682(v=vs.110).aspx

If your service is hosted in an Asp.net site you just need to update your web.config to set aspNetCompatibilityEnabled="true" on the serviceHostingEnvironment element:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
0

精彩评论

暂无评论...
验证码 换一张
取 消