【问题标题】:Get Windows Username from WCF server side从 WCF 服务器端获取 Windows 用户名
【发布时间】:2008-11-15 06:36:28
【问题描述】:

我对 Web 服务和 WCF 非常熟悉,并且我正在使用 Windows 集成身份验证 - 我如何在服务器端界面上获取用户名?我相信我应该实现自定义行为,或者可能是 WCF 会话?任何线索都会非常方便。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    尝试查看 ServiceSecurityContext.Current.WindowsIdentity

    【讨论】:

      【解决方案2】:

      这是一个服务代码的 sn-p,展示了如何检索和使用与 WCF 服务的调用者关联的 WindowsIdentity。

      此代码假设您接受大多数默认配置。它应该与命名管道或网络 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();
      
      }
      

      【讨论】:

      • p.Demand 将要求Thread.CurrentPrincipal 处于指定角色并且与ServiceSecurityContext.Current.WindowsIdentity.Name 具有相同的用户名。
      【解决方案3】:

      获取 WCF 服务调用者用户名:

      var callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;

      【讨论】:

        【解决方案4】:

        你试过WindowsIdentity.GetCurrent();吗?

        【讨论】:

        • 这给出了运行服务的身份 - 而不是调用服务的用户的身份
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多