using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Configuration;
namespace BussinessLayer
{
public class ADHelper
{
//备份服务器
private static string ADPath_DLback = ConfigurationSettings.AppSettings["ADPath_DLback"];
/// <summary>
/// 登陆函数
/// </summary>
/// <param name="username">用户名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public bool Login(string strUser, string strPsw)
{
if (IsAuthenticated(ADPath_BJ, strUser, strPsw))
{
return true;
}
else if (IsAuthenticated(ADPath_DL, strUser, strPsw))
{
return true;
}
else
{
return IsAuthenticated(ADPath_DLback, strUser, strPsw);
}
}
/// <summary>
/// AD 域认证
/// </summary>
/// <param name="ADPath">AD域服务器</param>
/// <param name="username">用户名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
private bool IsAuthenticated(string ADPath, string username, string pwd)
{
try
{
DirectoryEntry entry = new DirectoryEntry(ADPath, username, pwd);
// 绑定到本机 AdsObject 以强制身份验证。
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
// 更新目录中的用户的新路径
entry = new DirectoryEntry(result.Path);
}
catch
{
return false;
}
return true;
}
}
}