简体   繁体   English

从服务端访问WCF身份验证信息

[英]Access WCF Authentication information from the Service Side

I use this code to authenticate to my WCF Service: 我使用以下代码对我的WCF服务进行身份验证:

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? 是否可以通过WCF服务代码的方法来访问此信息? (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. PrimaryIdentity将包含一个“普通” IIdentity,并具有身份对象类所携带的所有字段(例如IsAuthenticated等)。

Marc

您是否尝试过研究ServiceSecurityContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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