Сложность получения объекта Active Directory через GUIDC#

Место общения программистов C#
Ответить
Anonymous
 Сложность получения объекта Active Directory через GUID

Сообщение Anonymous »

Я могу подключиться к Active Directory и получить GUID, используя SamaccountName. Я пробую пару разных подходов, как показано ниже.
Очевидно, я делаю что -то < /em> не так, но я не могу сказать что. Глядя на примеры (см. Источники в комментариях ниже), это должно просто работать .
public class ActiveDirectoryReader
{
private const string Host = "Redacted";
private const string Username = "Redacted";
private const string Password = "Redacted";
private const string SamName = "Redacted";
[NotNull] [ItemNotNull] private readonly Lazy _lazyLdap;

public ActiveDirectoryReader()
{
_lazyLdap = new Lazy(() =>
{
var ipAddress = Dns.GetHostAddresses(Host)[0]?.ToString()
?? throw new InvalidOperationException();
return $"LDAP://{ipAddress}";
});
}

private static string GuidToOctetString(Guid guid) =>
guid.ToByteArray().Aggregate("", (current, b) => current + @"\" + b.ToString("x2"));

public void Get()
{
SearchResult searchResult;
using (var searcher = new DirectorySearcher(
searchRoot: new DirectoryEntry(_lazyLdap.Value, Username, Password),
filter: $"(&(objectClass=user)(samAccountName={SamName}))",
propertiesToLoad: new[] { "samaccountname", "objectuserGuid" }
))
{
searchResult = searcher.FindOne();
}

var entry = searchResult?.GetDirectoryEntry() ?? throw new DataException();
Guid.TryParseExact(entry.NativeGuid, "N", out var guid);
var octetGuid = GuidToOctetString(guid);

//Try just as-is
using (var searcher = new DirectorySearcher(
searchRoot: new DirectoryEntry(_lazyLdap.Value, Username, Password),
filter: $"(&(objectClass=user)(objectGUID={guid}))",
propertiesToLoad: new[] { "samaccountname", "objectuserGuid" }
))
{
searchResult = searcher.FindOne();//returns null
}

//Source: https://kjellsj.blogspot.com/2005/03/ge ... using.html
//NOTE: I've tried using objectuserGuid instead of objectGuid in the filter. No difference, still returns null.
using (var searcher = new DirectorySearcher(
searchRoot: new DirectoryEntry(_lazyLdap.Value, Username, Password),
filter: $"(&(objectClass=user)(objectGUID={octetGuid}))",
propertiesToLoad: new[] { "samaccountname", "objectuserGuid" }
))
{
searchResult = searcher.FindOne();//returns null
}

//Source: https://stackoverflow.com/a/6658928
try
{
using (var searcher = new DirectorySearcher(
searchRoot: new DirectoryEntry($"{_lazyLdap.Value}/", Username, Password),
filter: $"(&(objectClass=user)(objectGuid={octetGuid}))",
propertiesToLoad: new[] { "samaccountname", "objectuserGuid" }
))
{
searchResult = searcher.FindOne();//returns error
}
}
catch (Exception e)
{
//Type: System.DirectoryServices.DirectoryServicesCOMException
//Message: There is no such object on the server.

Console.WriteLine(e);
System.Diagnostics.Debugger.Break();
}

//Try the last one but with octetGuid
try
{
using (var searcher = new DirectorySearcher(
searchRoot: new DirectoryEntry($"{_lazyLdap.Value}/", Username, Password),
filter: $"(&(objectClass=user)(objectGuid={octetGuid}))",
propertiesToLoad: new[] { "samaccountname", "objectuserGuid" }
))
{
searchResult = searcher.FindOne();//returns error
}
}
catch (Exception e)
{
//Type: System.DirectoryServices.DirectoryServicesCOMException
//Message: An invalid dn syntax has been specified.

Console.WriteLine(e);
System.Diagnostics.Debugger.Break();
}

return new ActiveDirectoryInfoDto(searchResult?.GetDirectoryEntry(), propertyName, propertyValue);
}
}


Подробнее здесь: https://stackoverflow.com/questions/796 ... t-via-guid
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»