67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
|
namespace BLL
|
|||
|
{
|
|||
|
using System;
|
|||
|
using System.DirectoryServices;
|
|||
|
|
|||
|
public class ADomainService
|
|||
|
{
|
|||
|
#region 是否连接到域
|
|||
|
/// <summary>
|
|||
|
/// </summary>
|
|||
|
/// <param name="domainName">域名或IP</param>
|
|||
|
/// <param name="userName">用户名</param>
|
|||
|
/// <param name="userPwd">密码</param>
|
|||
|
/// <returns>域</returns>
|
|||
|
public static DirectoryEntry IsConnected(string domainName, string userName, string userPwd)
|
|||
|
{
|
|||
|
DirectoryEntry domain = new DirectoryEntry();
|
|||
|
try
|
|||
|
{
|
|||
|
domain.Path = string.Format("LDAP://{0}", domainName);
|
|||
|
domain.Username = userName;
|
|||
|
domain.Password = BLL.CommonService.Base64Decode(userPwd);
|
|||
|
//domain.AuthenticationType = AuthenticationTypes.None;
|
|||
|
domain.RefreshCache();
|
|||
|
return domain;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
BLL.ErrLogInfo.WriteLog(ex.ToString());
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 域中是否存在组织单位
|
|||
|
/// <summary>
|
|||
|
/// </summary>
|
|||
|
/// <param name="entry"></param>
|
|||
|
/// <param name="ou"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static DirectoryEntry IsExistOU(DirectoryEntry entry, string domainName, string domainOU)
|
|||
|
{
|
|||
|
DirectoryEntry ou = new DirectoryEntry();
|
|||
|
try
|
|||
|
{
|
|||
|
string[] ouItem = domainOU.Split(new string[1] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
|||
|
foreach (var item in ouItem)
|
|||
|
{
|
|||
|
if (!item.Equals(domainName, StringComparison.OrdinalIgnoreCase) && !item.Equals(domainName + ".com", StringComparison.OrdinalIgnoreCase))
|
|||
|
{
|
|||
|
entry = entry.Children.Find("OU=" + item);
|
|||
|
ou = entry;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return ou;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
BLL.ErrLogInfo.WriteLog(ex.ToString());
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|