Làm cách nào để có danh sách đầy đủ các Nhóm trong Thư mục Hoạt động của tôi?ASP.NET Cách nhận Danh sách Nhóm trong Active Directory
19
A
Trả lời
33
Check-out System.DirectoryServices (Một tài liệu tham khảo ASP.NET 2.0):
C# -example để có được các nhóm:
using System.DirectoryServices;
public class test
{
private void main()
{
foreach (string @group in GetGroups())
{
Debug.Print(@group);
}
}
public List<string> GetGroups()
{
DirectoryEntry objADAM = default(DirectoryEntry);
// Binding object.
DirectoryEntry objGroupEntry = default(DirectoryEntry);
// Group Results.
DirectorySearcher objSearchADAM = default(DirectorySearcher);
// Search object.
SearchResultCollection objSearchResults = default(SearchResultCollection);
// Results collection.
string strPath = null;
// Binding path.
List<string> result = new List<string>();
// Construct the binding string.
strPath = "LDAP://stefanserver.stefannet.local";
//Change to your ADserver
// Get the AD LDS object.
try
{
objADAM = new DirectoryEntry(strPath);
objADAM.RefreshCache();
}
catch (Exception e)
{
throw e;
}
// Get search object, specify filter and scope,
// perform search.
try
{
objSearchADAM = new DirectorySearcher(objADAM);
objSearchADAM.Filter = "(&(objectClass=group))";
objSearchADAM.SearchScope = SearchScope.Subtree;
objSearchResults = objSearchADAM.FindAll();
}
catch (Exception e)
{
throw e;
}
// Enumerate groups
try
{
if (objSearchResults.Count != 0)
{
foreach (SearchResult objResult in objSearchResults)
{
objGroupEntry = objResult.GetDirectoryEntry();
result.Add(objGroupEntry.Name);
}
}
else
{
throw new Exception("No groups found");
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return result;
}
}
VB-dụ để có được các nhóm:
Imports System.DirectoryServices
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each group As String In GetGroups()
Debug.Print(group)
Next
End Sub
Public Function GetGroups() As List(Of String)
Dim objADAM As DirectoryEntry ' Binding object.
Dim objGroupEntry As DirectoryEntry ' Group Results.
Dim objSearchADAM As DirectorySearcher ' Search object.
Dim objSearchResults As SearchResultCollection ' Results collection.
Dim strPath As String ' Binding path.
Dim result As New List(Of String)
' Construct the binding string.
strPath = "LDAP://stefanserver.stefannet.local" 'Change to your ADserver
' Get the AD LDS object.
Try
objADAM = New DirectoryEntry(strPath)
objADAM.RefreshCache()
Catch e As Exception
Throw e
End Try
' Get search object, specify filter and scope,
' perform search.
Try
objSearchADAM = New DirectorySearcher(objADAM)
objSearchADAM.Filter = "(&(objectClass=group))"
objSearchADAM.SearchScope = SearchScope.Subtree
objSearchResults = objSearchADAM.FindAll()
Catch e As Exception
Throw e
End Try
' Enumerate groups
Try
If objSearchResults.Count <> 0 Then
Dim objResult As SearchResult
For Each objResult In objSearchResults
objGroupEntry = objResult.GetDirectoryEntry
result.Add(objGroupEntry.Name)
Next objResult
Else
Throw New Exception("No groups found")
End If
Catch e As Exception
Throw New Exception(e.Message)
End Try
Return result
End Function
End Class
1
Microsoft .NET Framework cung cấp thư viện chuẩn để làm việc với Active Directory: System.DirectoryServices namespace trong System.DirectoryServices.dll.
Microsoft khuyến cáo sử dụng hai lớp chính từ namespace System.DirectoryServices: DirectoryEntry và DirectorySearcher. Trong hầu hết các trường hợp, nó là đủ để chỉ sử dụng lớp DirectorySearcher.
Bạn có thể tìm thấy một số ví dụ trong số CodeProject article này.
Các vấn đề liên quan
- 1. Lấy danh sách thành viên nhóm (memberOf) trong Active Directory
- 2. Active Directory Danh sách OU của
- 3. ASP.NET Active Directory Search
- 4. Bắt các nhóm của người dùng trong Active Directory
- 5. Azure Active Directory Nhóm/Vai trò
- 6. Nhận biết giao dịch của Active Directory?
- 7. Cách lấy danh sách tất cả các tên miền trong Active Directory bằng C#
- 8. Nhận tư cách thành viên nhóm của người dùng từ Active Directory
- 9. TSQL: Làm thế nào để có được một danh sách các nhóm mà người dùng thuộc về trong Active Directory
- 10. Cách truy xuất SAMAccountName từ Active Directory
- 11. Chỉ nhận tài khoản đã kích hoạt từ Active Directory
- 12. Gerrit và Active Directory
- 13. Cách lấy danh sách Người dùng từ Active Directory theo các thuộc tính như Bộ
- 14. Đường dẫn Active Directory LDAP
- 15. Active Directory vs OpenLDAP
- 16. Xác thực dựa trên vai trò Asp.Net bằng cách sử dụng nhóm Bảo mật trong Active Directory
- 17. Active Directory User Group Memberships GroupPrincipal
- 18. Nhận các thành viên của một nhóm Active Directory đệ quy, tức là bao gồm các phân nhóm
- 19. Làm cách nào để bạn mạo danh người dùng Active Directory trong Powershell?
- 20. Làm cách nào để xác thực đối với Active Directory từ mã dịch vụ web ASP.NET?
- 21. Display thumbnailPhoto từ Active Directory trong PHP
- 22. C# Tạo OU trong Active Directory
- 23. Quản lý vai trò và nhóm người dùng trong .NET với Active Directory
- 24. C# Active Directory PrincipalContext/UserPrincipal.IsMemberOf lỗi
- 25. Làm cách nào để hạn chế teamcity.users cho các thành viên của nhóm Active Directory (LDAP)?
- 26. Cách xóa người dùng trong Active Directory bằng Java
- 27. Thiết lập SSL trong Active Directory cách thực hiện
- 28. Thêm và xóa người dùng khỏi các nhóm Active Directory trong .NET
- 29. Ví dụ sử dụng nhóm Active Directory/LDAP cho quyền \ vai trò trong Rails App
- 30. Lưu trữ dữ liệu ứng dụng trong Active Directory
lớn công cụ, làm việc như một say mê cho tôi! –
Cảm ơn Stefan cho bài viết tuyệt vời ... tôi đã thử sử dụng mã vb trong ứng dụng web của tôi nhưng dường như không hoạt động ... bạn có thể làm sáng tỏ một số ánh sáng không? mcuh đánh giá cao .._____ –
@mo, bạn đã thay đổi đường dẫn/URL để khớp * * AD-server của bạn chưa? Nó không phải là một máy chủ AD thực trong ví dụ của tôi. – Stefan