I have the user's SID as byte[]
within windowsPrincipal.getIdentity().getSid()
.
How can I get an Active Directory entry (DirectoryEntry) from the SID?
Searching Active Directory by SID using PowerShell The IncludeDeletedObjects parameter allows you to search for deleted objects in the Active Directory Recycle Bin. In our case, the AD object with the specified SID is a domain computer (see the objectClass attribute).
You can use the command line (cmd) to convert SID to username using the wmic command. Using the wmic command to get user account, specify the user SID in the where clause to get a user from SID.
SID (Security IDentifier) is a unique id number assigned to each user on windows computer, group or computer on domain-controlled network. You can get current user SID in Active Directory using PowerShell Get-LocalUser cmdlet or ad user SID using Get-ADUser cmdlet in PowerShell.
The easiest way I've found is using LDAP binding. Similar to what Nick Giles said. More info at 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
This can also be done in PowerShell, as long as you have .Net 3.5 or 4.0 available (see https://gist.github.com/882528 if you don't by default)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With