最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
ActiveDirectory学习日记(一)
时间:2008-04-26 编辑:简简单单 来源:一聚教程网
1:如何获取域中某个组的所有用户
private void Page_Load(object sender, System.EventArgs e)
...{
StringCollection groupMembers = this.GetGroupMembers("pardesifashions","Debugger Users");
foreach (string strMember in groupMembers)
...{
Response.Write("
" + strMember + "");
}
}
public StringCollection GetGroupMembers(string strDomain, string strGroup)
...{
StringCollection groupMemebers = new StringCollection();
try
...{
DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
SearchResultCollection coll = srch.FindAll();
foreach (SearchResult rs in coll)
...{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach( Object memberColl in resultPropColl["member"])
...{
DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
object obVal = userProps["sAMAccountName"].Value;
if (null != obVal)
...{
groupMemebers.Add(obVal.ToString());
}
}
}
}
catch (Exception ex)
...{
Trace.Write(ex.Message);
}
return groupMemebers;
}
2:获取某个AD中所有的域
private void Page_Load(object sender, System.EventArgs e)
...{
StringCollection adDomains = this.GetDomainList();
foreach (string strDomain in adDomains)
...{
Response.Write("
" + strDomain + "");
}
}
private StringCollection GetDomainList()
...{
StringCollection domainList = new StringCollection();
try
...{
DirectoryEntry en = new DirectoryEntry("LDAP://");
// Search for objectCategory type "Domain"
DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
SearchResultCollection coll = srch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
...{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach( object domainName in resultPropColl["name"])
...{
domainList.Add(domainName.ToString());
}
}
}
catch (Exception ex)
...{
Trace.Write(ex.Message);
}
return domainList;
}
3:获取当前登录用户的全名
private void Page_Load(object sender, System.EventArgs e)
...{
WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
Response.Write(GetFullName(p.Identity.Name));
}
private string GetFullName(string strLogin)
...{
string str = "";
// Parse the string to check if domain name is present.
int idx = strLogin.IndexOf('''');
if (idx == -1)
...{
idx = strLogin.IndexOf(''@'');
}
string strDomain;
string strName;
if (idx != -1)
...{
strDomain = strLogin.Substring(0, idx);
strName = strLogin.Substring(idx+1);
}
else
...{
strDomain = Environment.MachineName;
strName = strLogin;
}
DirectoryEntry obDirEntry = null;
try
...{
obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
object obVal = coll["FullName"].Value;
str = obVal.ToString();
}
catch (Exception ex)
...{
str = "";
Trace.Write(ex.Message);
}
return str;
}
private void Page_Load(object sender, System.EventArgs e)
...{
StringCollection groupMembers = this.GetGroupMembers("pardesifashions","Debugger Users");
foreach (string strMember in groupMembers)
...{
Response.Write("
" + strMember + "");
}
}
public StringCollection GetGroupMembers(string strDomain, string strGroup)
...{
StringCollection groupMemebers = new StringCollection();
try
...{
DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
SearchResultCollection coll = srch.FindAll();
foreach (SearchResult rs in coll)
...{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach( Object memberColl in resultPropColl["member"])
...{
DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
object obVal = userProps["sAMAccountName"].Value;
if (null != obVal)
...{
groupMemebers.Add(obVal.ToString());
}
}
}
}
catch (Exception ex)
...{
Trace.Write(ex.Message);
}
return groupMemebers;
}
2:获取某个AD中所有的域
private void Page_Load(object sender, System.EventArgs e)
...{
StringCollection adDomains = this.GetDomainList();
foreach (string strDomain in adDomains)
...{
Response.Write("
" + strDomain + "");
}
}
private StringCollection GetDomainList()
...{
StringCollection domainList = new StringCollection();
try
...{
DirectoryEntry en = new DirectoryEntry("LDAP://");
// Search for objectCategory type "Domain"
DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
SearchResultCollection coll = srch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
...{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach( object domainName in resultPropColl["name"])
...{
domainList.Add(domainName.ToString());
}
}
}
catch (Exception ex)
...{
Trace.Write(ex.Message);
}
return domainList;
}
3:获取当前登录用户的全名
private void Page_Load(object sender, System.EventArgs e)
...{
WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
Response.Write(GetFullName(p.Identity.Name));
}
private string GetFullName(string strLogin)
...{
string str = "";
// Parse the string to check if domain name is present.
int idx = strLogin.IndexOf('''');
if (idx == -1)
...{
idx = strLogin.IndexOf(''@'');
}
string strDomain;
string strName;
if (idx != -1)
...{
strDomain = strLogin.Substring(0, idx);
strName = strLogin.Substring(idx+1);
}
else
...{
strDomain = Environment.MachineName;
strName = strLogin;
}
DirectoryEntry obDirEntry = null;
try
...{
obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
object obVal = coll["FullName"].Value;
str = obVal.ToString();
}
catch (Exception ex)
...{
str = "";
Trace.Write(ex.Message);
}
return str;
}
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10