Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wcf

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

like image 246
user757207 Avatar asked Jul 19 '11 12:07

user757207


People also ask

What is HttpContext current user identity name?

It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.

Where is HttpContext user stored?

It is most likely stored in Managed Passwords: Click Start > Run. Enter "control userpasswords2"


2 Answers

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.

like image 193
blowdart Avatar answered Nov 02 '22 01:11

blowdart


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>
like image 39
TonyE Avatar answered Nov 02 '22 01:11

TonyE