简体   繁体   中英

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.

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

您是否尝试过研究ServiceSecurityContext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM