2008-11-15 46 views
20

对于Web服务和WCF我很绿,而且我正在使用Windows集成身份验证 - 如何在服务器端界面上获取用户名?我相信我应该实现一个自定义的行为,或者可能是WCF会话的东西?任何线索都会超级方便。从WCF服务器端获取Windows用户名

回答

9

这里的服务代码片段展示了如何获取并使用与WCF服务的调用相关的的WindowsIdentity。

此代码假设您正在接受配置的大部分默认值。它应该在命名管道或网络TCP绑定中没有任何问题。

p.Demand()将确定用户是否位于由permissionGroup变量指定的窗口组中。

private static void DemandManagerPermission() 
{ 
    // Verify the use has authority to proceed 
    string permissionGroup = ConfigurationManager.AppSettings["ManagerPermissionGroup"]; 
    if (string.IsNullOrEmpty(permissionGroup)) 
     throw new FaultException("Group permissions not set for access control."); 

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
    var p = new PrincipalPermission(ServiceSecurityContext.Current.WindowsIdentity.Name, permissionGroup, true); 
    p.Demand(); 

} 
+1

p.Demand将要求“Thread.CurrentPrincipal”处于指定角色AND具有与“ServiceSecurityContext.Current”相同的用户名.WindowsIdentity.Name`。 – Joe 2015-11-13 14:22:27

-2

你试过WindowsIdentity.GetCurrent();

+8

这给出了服务正在运行的标识 - 而不是用户调用服务的标识 – Cocowalla 2011-02-24 11:16:04

49

尝试寻找在ServiceSecurityContext.Current.WindowsIdentity

5

要获得WCF服务调用者的用户名:

VAR callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;

相关问题