Reputation: 30123
I'm using ASP.NET MVC and here's my Model:
public class AdContactInfo
{
public Guid Guid { get; set; }
public string FullName { get; set; }
}
Then using this code snippet, I'm trying to search for list of users:
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 });
}
}
I'm surprised that the line Guid = item.Guid
has an error saying item.Guid
is Nullable and cannot be converted to Guid
.
After some research, I didn't find any result that says Guid of User in ActiveDirectory can contain null. However, I found an article that says if I set the ContextType
to Machine it will always return to null.
I want to know why it is Nullable.
Upvotes: 2
Views: 301
Reputation: 1
I know this is an old question, but I found this today - https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/dd4dc725-021b-4c8c-a44a-49b3235836b7
No actual object has objectGUID equal to the NULL GUID. The root object has parent equal to the NULL GUID.
Upvotes: 0
Reputation: 755177
The Guid
property on the UserPrincipal
class is inherited from the base Principal
class, where it's defined as Nullable<Guid>
. That's just the way it is - you need to deal with this.
Why this was chosen to be defined as nullable - I don't know. From my experience with AD, all the objects you come across that descend from Principal
- groups, computers, users etc. - will have a GUID
assigned.
Upvotes: 2