如果我们引用System.DirectoryServices.AccountManagement.IdentityType
的文档,则会将IdentityType.Name
标识为“The identity is a name”,仅此而已。这是什么名字?它期望的格式是什么?
这不起作用:
UserPrincipal.FindByIdentity(myContext, IdentityType.Name, "John Doe");
这两个都没有:
UserPrincipal.FindByIdentity(myContext, IdentityType.Name, "Doe, John");
也不是这样:
UserPrincipal.FindByIdentity(myContext, IdentityType.Name, "jdoe"); //The username
答案 0 :(得分:3)
我不确定你的意思是“不起作用”,但我认为你的意思是它返回null并且不会抛出异常。
它的工作原理的简短回答:IdentityType.Name
映射到基础Name
对象的Principal
属性:Principal.Name Property on MSDN。如果您查看Principal
的其他属性,您会发现它们会映射到IdentityType
的其他枚举。
根据目录的设置方式,用户的Name
属性可能与您的预期不符。例如,“jdoe”实际上可能是用户的SamAccountName
属性,但不是Name
。这可能是这里发生的事情,或者你的目标是错误的上下文(非预期的域或本地机器)。我会以编程方式或通过启动dsa.msc来仔细检查用户的属性。
除非您想专门定位Name
属性,否则请考虑使用the other FindByIdentity
method that searches all of the IdentityType
enumerations。然后,如果你使用下面的代码并且“jdoe”实际上是SamAccountName
,你仍然会找到你正在寻找的用户:
var userPrincipal = UserPrincipal.FindByIdentity(context, "jdoe");