How to get groups, users from Active Directory with C#
来源:互联网 发布:ubuntu添加用户 编辑:程序博客网 时间:2024/04/30 22:15
Don’t forget “using System.DirectoryServices;”
1. How to get group from AD ?
this.Cursor = Cursors.WaitCursor;
DirectoryEntry objDE = GetDirectoryObject();
DirectorySearcher objDS = new DirectorySearcher(objDE);
objDS.SearchRoot = objDE;
// User inputs some special group name for search.
if (String.IsNullOrEmpty( UserInputGroupNameforSearch )) {
objDS.Filter = "(&(objectClass=group))";
} else {
objDS.Filter = "(&(objectClass=group)(cn=" + UserInputGroupNameforSearch + "*))";
}
objDS.PageSize = 1000;
SearchResultCollection colResults = objDS.FindAll();
if (colResults != null) {
foreach (SearchResult objResult in colResults) {
// insert group name to Listbox
this.lstGroup.Items.Add(GetProperty(objResult, "cn"));
}
}
private DirectoryEntry GetDirectoryObject() {
DirectoryEntry objDE;
string lstrADFullPath = "LDAP://" + YourDomainName
string lstrUser = ValidUserName;
string lstrPassword = ValidPassword;
objDE = new DirectoryEntry(lstrADFullPath, lstrUser, lstrPassword, AuthenticationTypes.Secure);
return objDE;
}
2. How to get users list from special group?
DirectoryEntry objGroup = GetDirectoryObject();
DirectorySearcher objDS = new DirectorySearcher(objGroup);
objDS.Filter = "(&(objectClass=group)(cn=" + GroupNameInListBox + "))";
objDS.SearchRoot = objGroup;
SearchResult results = objDS.FindOne();
if (results != null) {
System.DirectoryServices.ResultPropertyCollection pcoll = results.Properties;
int n = pcoll["member"].Count;
for (int i = 0; i < n; i++) {
lsMemberString = pcoll["member"][i].ToString();
DirectoryEntry deUser = new DirectoryEntry("LDAP://UserDomainName/" + lsMemberString, ValidUserName, ValidUserPassword, AuthenticationTypes.Secure);
// Show all users with valid email address
if (deUser != null && deUser.Properties["mail"] != null && deUser.Properties["mail"].Value != null && ! String.IsNullOrEmpty(deUser.Properties["mail"].Value.ToString())) {
YourControl.Add(deUser.Properties["cn"].Value, deUser.Properties["sAMAccountName"].Value, deUser.Properties["givenName"].Value, deUser.Properties["SN"].Value, deUser.Properties["mail"].Value, deUser.Properties["description"].Value);
}
deUser.Close();
}
}
}
objDS.Dispose();
objGroup.Dispose();
3. How to get users from AD?
Search users by FirstName, Lastname, Email and WindowsAccount
String lsFirstNameStr = String.Empty;
String lsLastNameStr = String.Empty;
String lsEmailStr = String.Empty;
String lsWindowsNameStr = String.Empty;
if (!String.IsNullOrEmpty( UserFirstName )) {
lsFirstNameStr = "(givenName=" + UserFirstName + "*)";
}
if (!String.IsNullOrEmpty(UserLastName)) {
lsLastNameStr = "(SN=" + UserLastName + "*)";
} else {
lsLastNameStr = "(!(SN=*))";
}
if (!String.IsNullOrEmpty(UserEmail)) {
lsEmailStr = "(mail=" + UserEmail + "*)";
} else {
lsEmailStr = "(mail=*)";
}
if (!String.IsNullOrEmpty(UserWindowsName)) {
lsWindowsNameStr = "(sAMAccountName=" + UserWindowsName + "*)";
}
DirectoryEntry objDE = GetDirectoryObject();
DirectorySearcher objDS = new DirectorySearcher(objDE);
objDS.SearchRoot = objDE;
objDS.Filter = "(&(objectClass=user)" + lsEmailStr + lsLastNameStr + lsFirstNameStr + lsWindowsNameStr + ")";
objDS.PageSize = 1000;
SearchResultCollection colResults = objDS.FindAll();
if (colResults != null) {
foreach (SearchResult objResult in colResults) {
YourControl.Add(GetProperty(objResult, "cn"), GetProperty(objResult, "sAMAccountName"), GetProperty(objResult, "givenName"), GetProperty(objResult, "SN"), GetProperty(objResult, "mail"), GetProperty(objResult, "description"));
}
}
objDS.Dispose();
objDE.Dispose();
private string GetProperty(SearchResult searchResult, string PropertyName) {
if (searchResult.Properties.Contains(PropertyName)) {
return searchResult.Properties[PropertyName][0].ToString();
} else {
return string.Empty;
}
}
- How to get groups, users from Active Directory with C#
- How to gather UDIDs from users with iOS 7
- Query Active Directory Users using C#
- How to perform AD(MS Active Directory) Authentication using JNDI from Java?
- How to mark volume groups as active or inactive
- How to get root access for other users with the visudo
- How to Understand Your Users with Personas
- How to authenticate against the Active Directory by using Forms authentication and Visual C# .NET
- How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
- How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
- How To Get Your First 1,000 Users
- How to get file from classpath
- How to get parameters from the URL?
- How to get IHTMLDocument2 from a HWND
- How to get bean from tag component
- How to get byte array from imageview
- How to get UITableView from UITableViewCell?
- How to get UITableView from UITableViewCell?
- 将字符串中的字符'*'移到串的前部分 的一个解法
- 学习ASP.NET的总结(二)
- 随便写写
- 随便写写
- 让程序崩溃时自动写minidump
- How to get groups, users from Active Directory with C#
- DEBIAN 架设 LAMP web服务器
- 抽象工厂(Abstract Factory)
- Firefox中添加Microsoft Academic搜索引擎
- Android开发指导文档(译)--Activity
- 基于Hadoop的数据挖掘项目mahout的源码编译
- 李开复成长的10个启发
- 企业电子文档管理产品大PK
- C学习笔记-1-unsigned char的表示范围