代码之家  ›  专栏  ›  技术社区  ›  Ana Betts

从WCF服务器端获取Windows用户名

  •  20
  • Ana Betts  · 技术社区  · 16 年前

    我对web服务和WCF非常熟悉,我使用的是Windows集成身份验证—如何在服务器端界面上获取用户名?我相信我应该实现一个定制的行为,或者可能与WCF会话相关的东西?任何线索都非常方便。

    4 回复  |  直到 16 年前
        1
  •  50
  •   to StackOverflow    16 年前

    尝试查看ServiceSecurityContext.Current.WindowsIdentity

        2
  •  10
  •   Mitch Baker    16 年前

    此代码假定您接受配置中的大多数默认值。它应该可以在命名管道或网络TCP绑定中正常工作。

    p.Demand()将确定用户是否在permissionGroup变量指定的windows组中。

    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();
    
    }
    
        3
  •  7
  •   Francisco Goldenstein    12 年前

    var callerUserName=ServiceSecurityContext.Current.WindowsIdentity.Name;

        4
  •  -3
  •   Joel Martinez    16 年前

    你试过了吗 WindowsIdentity.GetCurrent(); ?