Как получить атрибуты Active Directory, не представленные классом UserPrincipal

Я имею в виду, что сейчас я использую System.DirectoryServices.AccountManagement, и если я использую класс UserPrincipal, я вижу только имя, среднее имя и т.д.

поэтому в моих кодах это нравится

UserPrincipal myUser = new UserPrincipal(pc);
myUser.Name = "aaaaaa";
myUser.SamAccountName = "aaaaaaa";
.
.
.
.
myUser.Save();

Как я могу увидеть атрибут как мобильный или info?

Ответы

Ответ 2

В этом случае вам нужно идти на один уровень глубже - обратно в недра DirectoryEntry - путем захвата его от принципала пользователя:

DirectoryEntry de = (myUser.GetUnderlyingObject() as DirectoryEntry);

if(de != null)
{
   // go for those attributes and do what you need to do
}

Ответ 3

up.Mobile был бы идеальным, но, к сожалению, такого метода в классе UserPrincipal нет, поэтому вам нужно переключиться на DirectoryEntry, вызвав .GetUnderlyingObject().

static void GetUserMobile(PrincipalContext ctx, string userGuid)
{
    try
    {
        UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.Guid, userGuid);
        DirectoryEntry up_de = (DirectoryEntry)up.GetUnderlyingObject();
        DirectorySearcher deSearch = new DirectorySearcher(up_de);
        deSearch.PropertiesToLoad.Add("mobile");
        SearchResultCollection results = deSearch.FindAll();
        if (results != null && results.Count > 0)
        {
            ResultPropertyCollection rpc = results[0].Properties;
            foreach (string rp in rpc.PropertyNames)
            {
                if (rp == "mobile")
                    Console.WriteLine(rpc["mobile"][0].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}