RSolberg
RSolberg

Reputation: 26972

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?

Upvotes: 5

Views: 775

Answers (2)

Perplexed
Perplexed

Reputation: 123

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name

Upvotes: 2

Josh
Josh

Reputation: 69262

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.

Upvotes: 8

Related Questions