Учитывая SID пользователя, как я могу получить AD DirectoryEntry? - PullRequest
7 голосов
/ 17 августа 2011

У меня есть SID пользователя как byte [] в windowsPrincipal.getIdentity (). GetSid ().Как я могу получить запись активного каталога (DirectoryEntry) из SID?

Ответы [ 4 ]

9 голосов
/ 25 февраля 2014

Используйте класс SecurityIdentifier , чтобы преобразовать sid из формата byte [] в строку и затем привязать непосредственно к объекту:

DirectoryEntry OpenEntry(byte[] sidAsBytes)
{
    var sid = new SecurityIdentifier(sidAsBytes, 0);

    return new DirectoryEntry(string.Format("LDAP://<SID={0}>", sid.ToString()));
}
4 голосов
/ 07 октября 2011

Я нашел этот пример в c #

    // SID must be in Security Descriptor Description Language (SDDL) format
    // The PrincipalSearcher can help you here too (result.Sid.ToString())
    public void FindByIdentitySid()
    {
        UserPrincipal user = UserPrincipal.FindByIdentity(
            adPrincipalContext,
            IdentityType.Sid,
            "S-1-5-21-2422933499-3002364838-2613214872-12917");
        Console.WriteLine(user.DistinguishedName);
    }

Преобразовано в VB.NET:

    ' SID must be in Security Descriptor Description Language (SDDL) format
    ' The PrincipalSearcher can help you here too (result.Sid.ToString())
    Public Sub FindByIdentitySid()
        Dim user As UserPrincipal = UserPrincipal.FindByIdentity(adPrincipalContext,     IdentityType.Sid, "S-1-5-21-2422933499-3002364838-2613214872-12917")
        Console.WriteLine(user.DistinguishedName)
    End Sub

Очевидно, что тогда вы можете:

    dim de as new DirectoryEntry("LDAP://" & user.DistinguishedName)

Чтобы получитьSID = S-1-5-21- * (извините, VB.NET)

    ' Convert ObjectSID to a String

    ' http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/57452aab-4b68-4444-aefa-136b387dd06e

    Dim ADpropSid As Byte()
    ADpropSid = de.Properties("objectSid").Item(0)    
    ' in my test the byte field looks like this : 01 02 00 00 00 00.......37 02 00 00
    Dim SID As New System.Security.Principal.SecurityIdentifier(ADpropSid, 0)

Я еще не тестировал C # и не использовал конвертированную версию, но использовалвыше, чтобы вернуть SID в формате SDDL.

0 голосов
/ 23 апреля 2013

Самый простой способ, который я нашел, это использование привязки LDAP. Похоже на то, что сказал Ник Джайлс. Больше информации на MSDN

''' <summary>
''' Gets the DirectoryEntry identified by this SecurityIdentifier.
''' </summary>
''' <param name="id">The SecurityIdentifier (SID).</param>
<System.Runtime.CompilerServices.Extension()> _
Public Function GetDirectoryEntry(ByVal id As SecurityIdentifier) As DirectoryEntry
    Const sidBindingFormat As String = "LDAP://AOT/<SID={0}>"

    Return New DirectoryEntry(String.Format(sidBindingFormat, id.Value))
End Function
0 голосов
/ 17 февраля 2012

Это также можно сделать в PowerShell, если у вас есть доступный .Net 3.5 или 4.0 (см. https://gist.github.com/882528, если у вас нет по умолчанию)

add-type -assemblyname system.directoryservices.accountmanagement
$adPrincipalContext = 
    New-Object System.DirectoryServices.AccountManagement.PrincipalContext( 
    [System.DirectoryServices.AccountManagement.ContextType]::Domain)
$user = [system.directoryservices.accountmanagement.userprincipal]::findbyidentity(
    $adPrincipalContext
    , [System.DirectoryServices.AccountManagement.IdentityType]::Sid
    , "S-1-5-21-2422933499-3002364838-2613214872-12917")
$user.DisplayName
$user.DistinguishedName
...