NETCore 同步AD域组织和用户

BitAdminCore为没有自主开发框架的小团队,提供快速项目搭建及开发能力。

框架演示:http://bit.bitdao.cn

框架源码:https://github.com/chenyinxin/cookiecutter-bitadmin-core


 

不解释,直接上代码

1         //配置以下四个参数,开放389端口。
2         string domainName = "bitdao.cn";
3         string domainRoot = "组织单位";
4         string domainUser = "user";
5         string domainPass = "password";

 

  1   public ActionResult Sync()
  2         {
  3             try
  4             {
  5                 //连接域
  6                 DirectoryEntry domain = new DirectoryEntry();
  7                 domain.Path = string.Format("LDAP://{0}", domainName);
  8                 domain.Username = domainUser;
  9                 domain.Password = domainPass;
 10                 domain.AuthenticationType = AuthenticationTypes.Secure;
 11                 domain.RefreshCache();
 12 
 13                 DirectoryEntry entryOU = domain.Children.Find("OU=" + domainRoot);
 14                 DirectorySearcher mySearcher = new DirectorySearcher(entryOU, "(objectclass=organizationalUnit)"); //查询组织单位
 15                 DirectoryEntry root = mySearcher.SearchRoot;   //查找根OU
 16 
 17                 if (root.Properties.Contains("ou") && root.Properties.Contains("objectGUID"))
 18                 {
 19                     string rootOuName = root.Properties["ou"][0].ToString();
 20                     byte[] bGUID = root.Properties["objectGUID"][0] as byte[];
 21                     Guid id = new Guid(bGUID);
 22 
 23                     departments.Add(new SysDepartment() { DepartmentId = id, DepartmentCode = id.ToString(), DepartmentName = rootOuName, DepartmentFullName = rootOuName });
 24 
 25                     SyncSubOU(root, id);
 26                 }
 27 
 28                 //入库
 29                 foreach (var d in departments)
 30                 {
 31                     var department = dbContext.SysDepartment.Find(d.DepartmentId);
 32                     if (department == null)
 33                         dbContext.SysDepartment.Add(d);
 34                     else
 35                     {
 36                         department.DepartmentName = d.DepartmentName;
 37                         department.DepartmentFullName = d.DepartmentFullName;
 38                         department.ParentId = d.ParentId;
 39                     }
 40                     dbContext.SaveChanges();
 41                 }
 42                 foreach (var u in users)
 43                 {
 44                     var user = dbContext.SysUser.Find(u.UserId);
 45                     if (user == null)
 46                     {
 47                         u.CreateBy = u.UserId;
 48                         u.CreateTime = DateTime.Now;
 49                         dbContext.SysUser.Add(u);
 50                     }
 51                     else
 52                     {
 53                         user.UserCode = u.UserCode;
 54                         user.UserName = u.UserName;
 55                         user.DepartmentId = u.DepartmentId;
 56                         user.Mobile = u.Mobile;
 57                         user.Email = u.Email;
 58                         user.UpdateBy = u.UserId;
 59                         user.UpdateTime = DateTime.Now;
 60                     }
 61                     dbContext.SaveChanges();
 62                 }
 63 
 64 
 65                 return Json(new { Code = 0, Msg = "同步成功!" });
 66             }
 67             catch (Exception ex)
 68             {
 69                 LogHelper.SaveLog(ex);
 70                 return Json(new { Code = 1, Msg = "服务器异常,请联系管理员!" });
 71             }
 72         }
 73 
 74         List<SysUser> users = new List<SysUser>();
 75         List<SysDepartment> departments = new List<SysDepartment>();
 76 
 77         private void SyncSubOU(DirectoryEntry entry, Guid parentId)
 78         {
 79             foreach (DirectoryEntry subEntry in entry.Children)
 80             {
 81                 string entrySchemaClsName = subEntry.SchemaClassName;
 82 
 83                 string[] arr = subEntry.Name.Split('=');
 84                 string categoryStr = arr[0];
 85                 string nameStr = arr[1];
 86 
 87                 byte[] bGUID = subEntry.Properties["objectGUID"][0] as byte[];
 88                 Guid id = new Guid(bGUID);
 89 
 90                 switch (entrySchemaClsName)
 91                 {
 92                     case "organizationalUnit":
 93                         departments.Add(new SysDepartment() { DepartmentId = id, ParentId = parentId, DepartmentCode = id.ToString(), DepartmentName = nameStr, DepartmentFullName = nameStr });
 94 
 95                         SyncSubOU(subEntry, id);
 96                         break;
 97                     case "user":
 98                         users.Add(new SysUser()
 99                         {
100                             UserId = id,
101                             UserCode = subEntry.Properties["samaccountName"][0].ToString(),
102                             DepartmentId = parentId,
103                             UserName = subEntry.Properties["displayName"][0].ToString(),
104                             Email = subEntry.Properties.Contains("mail") ? subEntry.Properties["mail"][0].ToString() : "",
105                             Mobile = subEntry.Properties.Contains("telephoneNumber") ? subEntry.Properties["telephoneNumber"][0].ToString() : ""
106                         });
107 
108                         break;
109                 }
110             }
111         }

 

posted on 2018-09-27 19:25  陈银鑫  阅读(1920)  评论(1编辑  收藏  举报