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

后端 未结 2 582
不知归路
不知归路 2020-12-17 10:06

How can I access HttpContext.Current.User.Username from a web application in a WCF service?

相关标签:
2条回答
  • 2020-12-17 10:39

    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 讨论(0)
  • 2020-12-17 10:44

    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.

    0 讨论(0)
提交回复
热议问题