Nate
Nate

Reputation: 30636

Access WCF Authentication information from the Service Side

I use this code to authenticate to my WCF Service:

proxy.ClientCredentials.UserName.UserName = "test";
proxy.ClientCredentials.UserName.Password = "pass";

Is there any way to access this information from within a method of my WCF Service code? (I'm not interested in the password used, more the username for audit purposes.)

I'm trying to determine the identity of the user calling the method without changing the method signiture to include another parameter.

Upvotes: 1

Views: 1381

Answers (2)

marc_s
marc_s

Reputation: 754200

You can retrieve the user name of the caller like this:

ServiceSecurityContext ssc = ServiceSecurityContext.Current;

if (!ssc.IsAnonymous && ssc.PrimaryIdentity != null)
{
    string userName = ServiceSecurityContext.Current.PrimaryIdentity.Name;
}

The PrimaryIdentity will contain a "normal" IIdentity and has all the fields (like IsAuthenticated etc.) that the identity object class carries.

Marc

Upvotes: 4

tomasr
tomasr

Reputation: 13849

Have you tried looking into the ServiceSecurityContext?

Upvotes: 3

Related Questions