C# ActiveDirectory中用户的Guid可为空吗?
我正在使用ASP.NET MVC,以下是我的模型:C# ActiveDirectory中用户的Guid可为空吗?,c#,asp.net,active-directory,C#,Asp.net,Active Directory,我正在使用ASP.NET MVC,以下是我的模型: public class AdContactInfo { public Guid Guid { get; set; } public string FullName { get; set; } } 然后使用此代码段,我尝试搜索用户列表: using (var searcher = new PrincipalSearcher(new UserPrincipal(principal) { DisplayName = term +
public class AdContactInfo
{
public Guid Guid { get; set; }
public string FullName { get; set; }
}
然后使用此代码段,我尝试搜索用户列表:
using (var searcher = new PrincipalSearcher(new UserPrincipal(principal) { DisplayName = term + "*" }))
{
var result = searcher.FindAll();
foreach (var item in result)
{
list.Add(new AdContactInfo
{ FullName = item.Name,
Guid = item.Guid });
}
}
我很惊讶,行Guid=item.Guid
有一个错误,表示item.Guid
可为空,无法转换为Guid
经过一些研究,我并没有发现任何结果表明ActiveDirectory中用户的Guid可以包含null。然而,我发现了一个提示,如果我将ContextType
设置为Machine,它将始终返回null
我想知道为什么它可以为空 类
UserPrincipal
上的Guid
属性继承自基本Principal
类,在该类中定义为Nullable
。事情就是这样-你需要处理这个问题
为什么选择将其定义为可空-我不知道。根据我对AD的经验,您遇到的所有从
Principal
派生的对象(组、计算机、用户等)都将分配GUID
。用户Principal类上的GUID
属性从基本Principal
类继承而来,其中它被定义为可为空
。事情就是这样-你需要处理这个问题。为什么选择将其定义为可空-我不知道。根据我在AD方面的经验,您遇到的所有从Principal
派生的对象—组、计算机、用户等—都将分配一个GUID