欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

NETCore 同步AD域组织和用户

程序员文章站 2022-12-25 11:53:15
BitAdminCore为没有自主开发框架的小团队,提供快速项目搭建及开发能力。 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookiecutter-bitadmin-core 不解释,直接上代码 ......

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

框架演示:

框架源码:


 

不解释,直接上代码

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         }