user757207
user757207

Reputation: 303

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

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

Upvotes: 19

Views: 19749

Answers (2)

TonyE
TonyE

Reputation: 149

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>

Upvotes: 1

blowdart
blowdart

Reputation: 56550

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.

Upvotes: 35

Related Questions