9

ユーザーの SID は にbyte[]ありますwindowsPrincipal.getIdentity().getSid()。SID から Active Directory エントリ (DirectoryEntry) を取得するにはどうすればよいですか?

4

4 に答える 4

11

SecurityIdentifierクラスを使用して sid を byte[] 形式から文字列に変換し、オブジェクトに直接バインドします。

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

    return new DirectoryEntry(string.Format("LDAP://<SID={0}>", sid.ToString()));
}
于 2014-02-24T22:21:26.820 に答える
4

私はこの例を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 形式で返しました。

于 2011-10-06T22:12:04.990 に答える
2

私が見つけた最も簡単な方法は、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
于 2013-04-23T17:50:25.827 に答える
1

これは、.Net 3.5 または 4.0 が利用可能である限り、PowerShell でも実行できます (デフォルトで利用できない場合は、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
于 2012-02-17T00:33:54.883 に答える