【问题标题】:Comparing WindowsIdentity.Current.Name on Server比较服务器上的 WindowsIdentity.Current.Name
【发布时间】:2012-07-27 15:00:15
【问题描述】:

我目前正在审查具有 wcf 服务和客户端应用程序的系统,该系统使用 Windows Auth 通过 tcp 连接到它。

在某些操作中,需要将用户名存储在对象属性中,将其发送到服务并将其与从客户端传递的凭据进行比较。该对象将被进一步处理。

客户最近发现,这并不总是按预期工作。我调查了这个问题,发现在某些情况下,客户端和服务器端使用了不同的用户名(大小写的用法不同)。

我已经安装了一个解决方法(显然在 string.Equals 中使用了 IgnoreCase 选项)但是我对真正的修复很感兴趣。

在客户端它使用 WindowsIdentity.Current.Name 作为字符串,在服务端它使用 ServiceSecurityContext.Current.WindowsIdentity.Name 作为字符串。

进行这种比较的正确方法是什么?

【问题讨论】:

    标签: c# .net wcf windows-identity


    【解决方案1】:

    客户端和服务器是否在同一个域中?如果是这样,请存储并比较 WindowsIdentity 对象上的 User 属性而不是用户名。这是用户的安全标识符 (SID),它是(简化)Windows 世界中唯一的数字用户 ID。

    您可以使用 Value 属性存储 SecurityIdentifier 对象的字符串表示形式:

    string storedSid = WindowsIdentity.Current.User.Value;
    

    然后您可以使用它重构一个 SecurityIdentifier,并在服务端进行比较:

    SecurityIdentifier sid = new SecurityIdentifier(storedSid);
    // SecurityIdentifer defines operator ==
    bool equalUsers = sid == ServiceSecurityContext.Current.WindowsIdentity.User;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      相关资源
      最近更新 更多