Tôi có SID của người dùng là byte [] trong windowsPrincipal.getIdentity(). GetSid(). Làm thế nào tôi có thể nhận được một mục nhập thư mục hoạt động (DirectoryEntry) từ SID?Cho SID của người dùng, làm thế nào tôi có thể nhận được AD DirectoryEntry?
5
A
Trả lời
4
tôi thấy ví dụ này trong 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);
}
Chuyển Đổi Sang 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
Rõ ràng bạn có thể sau đó:
dim de as new DirectoryEntry("LDAP://" & user.DistinguishedName)
Để có được SID = S-1 -5-21- * (xin lỗi 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)
Tôi chưa thử nghiệm C# hoặc đã sử dụng phiên bản đã chuyển đổi, nhưng đã sử dụng ở trên để trả về SID ở định dạng SDDL.
0
này cũng có thể được thực hiện trong PowerShell, miễn là bạn có Net 3.5 hoặc 4.0 có sẵn (xem https://gist.github.com/882528 nếu bạn không theo mặc định)
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
0
Cách đơn giản nhất tôi đã tìm thấy là sử dụng LDAP ràng buộc. Tương tự như những gì Nick Giles đã nói. Xem thêm thông tin tại 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
6
Sử dụng các lớp SecurityIdentifier để chuyển đổi sid từ byte [] định dạng chuỗi và sau đó liên kết trực tiếp đến đối tượng:
DirectoryEntry OpenEntry(byte[] sidAsBytes)
{
var sid = new SecurityIdentifier(sidAsBytes, 0);
return new DirectoryEntry(string.Format("LDAP://<SID={0}>", sid.ToString()));
}