关于Active Directory:
Active Directory是指Windows网络中的目录服务。它有两个作用:1.目录服务功能。2.集中式管理。我们这里就只讨论第二种作用,Active Directory可以集中管理对网络资源的访问,并允许用户只登陆一次就能访问在Active Directory上的所有资源。更重要的是,该技术还可以用来确保只有那些经过授权的用户或应用程序方可获准在具备安全保障的前提下针对相关资源实施访问调用。
好了,其它的我就暂时不写了,呵呵
下面说如何使用AD认证,首先可以根据用户名和密码来访问域
/// <summary>
/// 验证AD用户是否登录成功
/// </summary>
/// <param name="domain"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
public bool TryAuthenticate(string domain, string userName, string password)
{
bool isLogin = false;
try
{
DirectoryEntry entry = new DirectoryEntry(string.Format("LDAP://{0}", domain), userName, password);
entry.RefreshCache();
isLogin = true;
}
catch
{
isLogin = false;
}
return isLogin;
}
接下来,根据指定的用户名获取它所对应的Groups
/// <summary>
/// 取 userName 所对应的 Groups
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public List<string> GetADGroups(string userName)
{
List<string> groups = new List<string>();
string domain = ConfigurationManager.AppSettings["Domain"]; //配置文件里读取domain
string adminUsername = ConfigurationManager.AppSettings["AdminUserName"]; //配置文件里读取Admin 的 userName
string adminPassword = ConfigurationManager.AppSettings["AdminPassword"]; //配置文件里读取domain 的 Password
try
{
var entry = new DirectoryEntry(string.Format("LDAP://{0}", domain), adminUsername, adminPassword);
entry.RefreshCache();
DirectorySearcher search = new DirectorySearcher(entry);
search.PropertiesToLoad.Add("memberof");
search.Filter = string.Format("sAMAccountName={0}", userName);
SearchResult result = search.FindOne();
if (result != null)
{
ResultPropertyValueCollection c = result.Properties["memberof"];
foreach (var a in c)
{
string temp = a.ToString();
Match match = Regex.Match(temp, @"CN=/s*(?<g>/w*)/s*.");
groups.Add(match.Groups["g"].Value);
}
}
}
catch
{
}
return groups;
}
下面有几个关于Active Directory的资源
Active Directory Users, Computers, and Groups
Howto: (Almost) Everything In Active Directory via C#
C# Asynchronous Multi-Threaded Active Directory User/Group Browser
Searching Active Directory for Users and Groups