本人使用了,管用。
using System.DirectoryServices;
protected void Button1_Click(object sender, EventArgs e)
{
string strUserName = txtUserName.Text.Trim();
string strPassWord = txtPassWord.Text.Trim();
bool isAccount = ADLogin(strUserName, strPassWord);
if (isAccount)
{
lblMsg.Text = "合法用户";
}
else
{
lblMsg.Text = "非法用户";
}
}
public bool ADLogin(string userName, string password)
{
//string domain = System.Configuration.ConfigurationManager.AppSettings["AD_Domain"];
(局域网下的验证)
string domain = "LDAP://xxx.xx.xx.xx"; //你的IP地址 (互联网上的验证)
try
{
DirectoryEntry entry = new DirectoryEntry(domain, userName, password);
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = string.Format("(SAMAccountName={0})", userName);
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (result == null)
return false;
}
catch (Exception ex)
{
//log.Error(ex);
return false;
}
return true;
}