Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you identify authentcated user in WCF?

I have a WCF service that will be using basic authentication and would like to be able identify "who" is trying to use the service. I know that the HttpContext.Current is NULL and in the WCF service, but do not know what the alternative is to get the username.

For the website, I can use:

userName = HttpContext.Current.Request.ServerVariables["LOGON_USER"];

How do I get userName in the WCF Service?

like image 959
RSolberg Avatar asked May 19 '10 23:05

RSolberg


People also ask

How do I authenticate a user in WCF?

To make authentication of WCF service more secure use server certificate for authentication. If certificate is available include it in WCF server otherwise we can also create self-signed certificate from IIS.

How do I pass credentials to WCF services for Windows authentication?

To configure a service to authenticate its clients using Windows Domain username and passwords use the WSHttpBinding and set its Security. Mode property to Message . In addition you must specify an X509 certificate that will be used to encrypt the username and password as they are sent from the client to the service.

What is the difference between Negotiate and NTLM authentication?

Negotiate authentication automatically selects between the Kerberos protocol and NTLM authentication, depending on availability. The Kerberos protocol is used if it is available; otherwise, NTLM is tried. Kerberos authentication significantly improves upon NTLM.


2 Answers

Something like this maybe?

string login = OperationContext.Current
                               .ServiceSecurityContext
                               .PrimaryIdentity
                               .Name;

Obviously it helps to check for null reference exceptions along that path but you get the idea.

like image 142
Josh Avatar answered Oct 05 '22 23:10

Josh


OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name

like image 29
Perplexed Avatar answered Oct 05 '22 23:10

Perplexed