324 lines
12 KiB
C#
324 lines
12 KiB
C#
|
namespace BLL
|
|||
|
{
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Web.UI.WebControls;
|
|||
|
using System.Collections;
|
|||
|
using System.Timers;
|
|||
|
using System.DirectoryServices;
|
|||
|
|
|||
|
public class ADomainService
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 获取AD域信息
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public static Model.Sys_ADomain getADomain()
|
|||
|
{
|
|||
|
return Funs.DB.Sys_ADomain.FirstOrDefault();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 删除AD域信息
|
|||
|
/// </summary>
|
|||
|
/// <param name="FlowSetId"></param>
|
|||
|
public static void DeleteADomain()
|
|||
|
{
|
|||
|
var aDomain = from x in Funs.DB.Sys_ADomain select x;
|
|||
|
if (aDomain.Count() > 0)
|
|||
|
{
|
|||
|
Funs.DB.Sys_ADomain.DeleteAllOnSubmit(aDomain);
|
|||
|
Funs.DB.SubmitChanges();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 增加AD域设置信息
|
|||
|
/// </summary>
|
|||
|
/// <param name="FlowProjectSetName"></param>
|
|||
|
/// <param name="def"></param>
|
|||
|
public static void AddADomain(Model.Sys_ADomain aDomain)
|
|||
|
{
|
|||
|
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
|
|||
|
{
|
|||
|
aDomain.ADomainId = SQLHelper.GetNewID();
|
|||
|
db.Sys_ADomain.InsertOnSubmit(aDomain);
|
|||
|
db.SubmitChanges();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region 是否连接到域
|
|||
|
/// <summary>
|
|||
|
/// </summary>
|
|||
|
/// <param name="domainName">域名或IP</param>
|
|||
|
/// <param name="userName">用户名</param>
|
|||
|
/// <param name="userPwd">密码</param>
|
|||
|
/// <param name="entry">域</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 = userPwd;
|
|||
|
domain.AuthenticationType = AuthenticationTypes.Secure;
|
|||
|
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
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 导入AD域用户
|
|||
|
/// </summary>
|
|||
|
public static void ADomainUserIn()
|
|||
|
{
|
|||
|
var adomain = ADomainService.getADomain();
|
|||
|
if (adomain != null)
|
|||
|
{
|
|||
|
DirectoryEntry domain = BLL.ADomainService.IsConnected(adomain.DomainName, adomain.UserName, adomain.Password);
|
|||
|
if (domain != null)
|
|||
|
{
|
|||
|
DirectoryEntry rootOU = BLL.ADomainService.IsExistOU(domain, adomain.DomainName, adomain.RootOU);
|
|||
|
if (rootOU != null)
|
|||
|
{
|
|||
|
SyncAll(rootOU);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
#region 同步所有事件
|
|||
|
/// <summary>
|
|||
|
/// </summary>
|
|||
|
/// <param name="entryOU"></param>
|
|||
|
public static void SyncAll(DirectoryEntry entryOU)
|
|||
|
{
|
|||
|
DirectorySearcher mySearcher = new DirectorySearcher(entryOU, "(objectClass=organizationalUnit)"); //查询组织单位
|
|||
|
DirectoryEntry root = mySearcher.SearchRoot; //查找根OU
|
|||
|
SyncRootOU(root);
|
|||
|
foreach (var item in list)
|
|||
|
{
|
|||
|
Model.Sys_User user = new Model.Sys_User();
|
|||
|
user.Account = item.Account;
|
|||
|
user.UserCode = item.UserCode;
|
|||
|
user.Password = item.Password;
|
|||
|
user.UserName = item.UserName;
|
|||
|
if (!String.IsNullOrEmpty(item.RoleName))
|
|||
|
{
|
|||
|
var role = BLL.RoleService.getRoleByName(item.RoleName);
|
|||
|
if (role != null)
|
|||
|
{
|
|||
|
user.RoleId = role.RoleId;
|
|||
|
user.IsOffice = true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
user.IsPost = item.IsPost;
|
|||
|
if (!String.IsNullOrEmpty(item.UnitName))
|
|||
|
{
|
|||
|
var unit = BLL.UnitService.getUnitByUnitName(item.UnitName);
|
|||
|
if (unit != null)
|
|||
|
{
|
|||
|
user.UnitId = unit.UnitId;
|
|||
|
}
|
|||
|
}
|
|||
|
if (!String.IsNullOrEmpty(item.DepartName))
|
|||
|
{
|
|||
|
var dep = BLL.DepartService.getDepartByDepartName(item.DepartName);
|
|||
|
if (dep != null)
|
|||
|
{
|
|||
|
user.DepartId = dep.DepartId;
|
|||
|
}
|
|||
|
}
|
|||
|
////根据登录名查询用户信息
|
|||
|
var userSelect = BLL.UserService.GetUserByAccount(item.Account);
|
|||
|
if (userSelect == null) ///不存在则增加
|
|||
|
{
|
|||
|
BLL.UserService.AddUser(user);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
////存在则更新
|
|||
|
//user.UserId = userSelect.UserId;
|
|||
|
//user.UserCode = userSelect.UserCode;
|
|||
|
//user.Password = userSelect.Password;
|
|||
|
//user.RoleId = userSelect.RoleId;
|
|||
|
//user.UnitId = userSelect.UnitId;
|
|||
|
//user.DepartId = userSelect.DepartId;
|
|||
|
//BLL.UserService.UpdateUser(user);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 同步根组织单位
|
|||
|
/// <summary>
|
|||
|
/// </summary>
|
|||
|
/// <param name="entry"></param>
|
|||
|
public static void SyncRootOU(DirectoryEntry entry)
|
|||
|
{
|
|||
|
if (entry.Properties.Contains("ou") && entry.Properties.Contains("objectGUID"))
|
|||
|
{
|
|||
|
byte[] bGUID = entry.Properties["objectGUID"][0] as byte[];
|
|||
|
string id = BitConverter.ToString(bGUID);
|
|||
|
SyncSubOU(entry, id);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 同步下属组织单位及下属用户
|
|||
|
/// <summary>
|
|||
|
/// </summary>
|
|||
|
/// <param name="entry"></param>
|
|||
|
/// <param name="parentId"></param>
|
|||
|
public static void SyncSubOU(DirectoryEntry entry, string parentId)
|
|||
|
{
|
|||
|
foreach (DirectoryEntry subEntry in entry.Children)
|
|||
|
{
|
|||
|
string entrySchemaClsName = subEntry.SchemaClassName;
|
|||
|
|
|||
|
string[] arr = subEntry.Name.Split('=');
|
|||
|
string categoryStr = arr[0];
|
|||
|
string nameStr = arr[1];
|
|||
|
string id = string.Empty;
|
|||
|
|
|||
|
if (subEntry.Properties.Contains("objectGUID")) //SID
|
|||
|
{
|
|||
|
byte[] bGUID = subEntry.Properties["objectGUID"][0] as byte[];
|
|||
|
|
|||
|
id = BitConverter.ToString(bGUID);
|
|||
|
}
|
|||
|
|
|||
|
bool isExist = list.Exists(d => d.Id == id);
|
|||
|
|
|||
|
switch (entrySchemaClsName)
|
|||
|
{
|
|||
|
case "organizationalUnit":
|
|||
|
SyncSubOU(subEntry, id);
|
|||
|
break;
|
|||
|
case "user":
|
|||
|
string account = string.Empty;
|
|||
|
string userCode = string.Empty;
|
|||
|
string password = BLL.Funs.EncryptionPassword(BLL.Const.Password);
|
|||
|
string userName = string.Empty;
|
|||
|
string roleId = string.Empty;
|
|||
|
bool isPost = true;
|
|||
|
string unitId = string.Empty;
|
|||
|
string department = string.Empty;
|
|||
|
|
|||
|
if (subEntry.Properties.Contains("samaccountName"))
|
|||
|
{
|
|||
|
account = subEntry.Properties["samaccountName"][0].ToString();
|
|||
|
if (subEntry.Properties["initials"].Value != null)
|
|||
|
{
|
|||
|
userCode = subEntry.Properties["initials"].Value.ToString();
|
|||
|
}
|
|||
|
if (subEntry.Properties["displayName"].Value != null)
|
|||
|
{
|
|||
|
userName = subEntry.Properties["displayName"].Value.ToString();
|
|||
|
}
|
|||
|
//if (subEntry.Properties["title"].Value != null)
|
|||
|
//{
|
|||
|
// roleId = subEntry.Properties["title"].Value.ToString();
|
|||
|
//}
|
|||
|
if (subEntry.Properties["company"].Value != null)
|
|||
|
{
|
|||
|
unitId = subEntry.Properties["company"].Value.ToString();
|
|||
|
}
|
|||
|
if (subEntry.Properties["department"].Value != null)
|
|||
|
{
|
|||
|
department = subEntry.Properties["department"].Value.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (!isExist && !String.IsNullOrEmpty(account))
|
|||
|
{
|
|||
|
list.Add(new AdModel(id, account, userCode, password, userName, roleId, isPost, unitId, department));
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Ad域实体
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public static List<AdModel> list = new List<AdModel>();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Ad域实体
|
|||
|
/// </summary>
|
|||
|
public class AdModel
|
|||
|
{
|
|||
|
public AdModel(string id, string account, string userCode, string password, string userName,
|
|||
|
string roleName, bool? isPost, string unitName, string departName)
|
|||
|
{
|
|||
|
Id = id;
|
|||
|
Account = account;
|
|||
|
UserCode = userCode;
|
|||
|
Password = password;
|
|||
|
UserName = userName;
|
|||
|
RoleName = roleName;
|
|||
|
IsPost = isPost;
|
|||
|
UnitName = unitName;
|
|||
|
DepartName = departName;
|
|||
|
}
|
|||
|
|
|||
|
public string Id { get; set; }
|
|||
|
public string Account { get; set; }
|
|||
|
public string UserCode { get; set; }
|
|||
|
public string Password { get; set; }
|
|||
|
public string UserName { get; set; }
|
|||
|
public string RoleName { get; set; }
|
|||
|
public bool? IsPost { get; set; }
|
|||
|
public string UnitName { get; set; }
|
|||
|
public string DepartName { get; set; }
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|