views:

71

answers:

2

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?

+6  A: 

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.

Josh Einstein
Thanks, will test it out...
RSolberg
Watch out - the PrimaryIdentity could be NULL! Just make sure to check for that....
marc_s
Indeed, I did point that out though under my code snippet.
Josh Einstein
+2  A: 

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name

Perplexed
Thanks! He got you beat by 15 seconds I think...
RSolberg