【问题标题】:Principal.IsInRole returns different results for the same WindowsPrincipal, depending on how it is constructedPrincipal.IsInRole 为同一个 WindowsPrincipal 返回不同的结果,具体取决于它的构造方式
【发布时间】:2017-09-01 15:47:05
【问题描述】:

我有两种方法来构造同一个 WindowsPrincipal 对象

根据我的构造方式,principal.IsInRole() 得到不同的结果

这是我的代码:

var principal1 = new WindowsPrincipal(WindowsIdentity.GetCurrent());
var principal2 = new WindowsPrincipal(new WindowsIdentity("myName"));

principal1.IsInRole("groupName") :  returns false
principal2.IsInRole("groupName") :  returns true

principal1.Identity.Nameprincipal2.Identity.Name 是一样的。

知道发生了什么吗?

【问题讨论】:

标签: c# .net windows-identity


【解决方案1】:

这两个原则彼此不同。因为您正在选择当前用户并创建 new 用户。如果您从 windowsidentity 中查看委托人列表,您将看到,您创建的委托人 2 是新的并且没有分配任何组

var groupNames1 = from id in WindowsIdentity.GetCurrent().Groups
                 select id.Translate(typeof(NTAccount)).Value;

var groupNames2 = from id in (new WindowsIdentity("myName")).Groups
                 select id.Translate(typeof(NTAccount)).Value;

您将看到 groupNames1 和 groupNames2 不同的组集。

【讨论】:

  • principal2 确实有组。与 principal1 相同的编号,但它们是不同的组。既然两个主体同名,他们不应该有相同的组吗?
猜你喜欢
  • 2014-04-03
  • 1970-01-01
  • 2022-10-12
  • 2022-07-22
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多