Я могу подключиться к 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
Сложность получения объекта Active Directory через GUID ⇐ C#
Место общения программистов C#
-
Anonymous
1750362578
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/get-ad-objects-by-guid-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);
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79672219/difficulty-getting-active-directory-object-via-guid[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия