Skip to main content
 首页 » 编程设计

c#之如何在 WCF 服务上使用 Windows 身份验证访问服务器端的用户名

2024年02月23日18bonelee

我有一个 Silverlight 应用程序和一个使用 Windows 身份验证的 WCF 服务。 我的用户得到正确的身份验证。我可以在客户端访问他们的用户名和角色。 但是我如何在服务器端做同样的事情呢? 这个我试过了

ServiceSecurityContext.Current.WindowsIdentity 

和

IIdentity caller = ServiceSecurityContext.Current.PrimaryIdentity; 

这是我在 stackoverflow 上找到的类似问题的答案,但无法编译。 我收到“ServiceSecurityContext 不包含当前的定义”异常。

那么如何在服务器端获取用户呢?

请您参考如下方法:

public static string GetUserIdentity() 
{ 
   return OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name; 
} 

OperationContext 在 System.ServiceModel 命名空间中。