码迷,mamicode.com
首页 > Web开发 > 详细

NETCore 同步AD域组织和用户

时间:2018-09-27 19:45:22      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:.com   csu   tco   ret   byte   hone   源码   contains   refresh   

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         }

 

NETCore 同步AD域组织和用户

标签:.com   csu   tco   ret   byte   hone   源码   contains   refresh   

原文地址:https://www.cnblogs.com/chenyinxin/p/9715081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!