火星wap建站python创建网页
news/
2025/9/22 20:29:19/
文章来源:
火星wap建站,python创建网页,文库网站开发教程,wordpress发送邮箱验证码做企业应用开发难免会跟 AD 打交道#xff0c;在之前的 dotNET FrameWork 时代#xff0c;通常使用 System.DirectoryServices 的相关类来操作 AD #xff0c;在 dotNET Core 中没有这个命名空间#xff0c;在张善友大佬的推荐下#xff0c;知道了 Novell.Directory.Ldap。… 做企业应用开发难免会跟 AD 打交道在之前的 dotNET FrameWork 时代通常使用 System.DirectoryServices 的相关类来操作 AD 在 dotNET Core 中没有这个命名空间在张善友大佬的推荐下知道了 Novell.Directory.Ldap。操作 AD通常有两种常见的场景将第三方数据源数据人事系统同步到 AD 中将 AD 数据同步到自己的数据库中本文将介绍在 dotNET Core 中使用 Novell.Directory.Ldap 将 AD 数据同步到数据库的操作。环境dotNET Core2.1Novell.Directory.Ldap.NETStandard2_03.1.0安装 Novell.Directory.Ldap 包在 VS2019 中添加 NuGet 包引用如下图安装完成后在类中添加using Novell.Directory.Ldap;引用便可使用相关的 API 方法了。同步思路1、连接 AD基本操作同步方法public bool Sync()
{ADConnect();if (_connection null){throw new Exception(AD连接错误请确认AD相关信息配置正确!);}bool result true;ListLdapEntry entryList this.GetRootEntries(_adPaths, _adHost);_org new Org();_user new User();Org rootOrg _org.GetRootOrg();foreach (LdapEntry entry in entryList){SyncDirectoryEntry(entry, rootOrg, entry);}return result;
}
连接 ADpublic bool ADConnect()
{_adHost 192.168.16.160;string adAdminUserName administrator;string adAdminPassword 123456;_adPaths new string[] { OUoec2003,DCCOM,DCcn };if ((string.IsNullOrEmpty(_adHost) || string.IsNullOrEmpty(adAdminUserName)) ||string.IsNullOrEmpty(adAdminPassword)){return false;}try{_connection new LdapConnection();_connection.Connect(_adHost, LdapConnection.DEFAULT_PORT);_connection.Bind(adAdminUserName, adAdminPassword);}catch{return false;}return true;
}
递归操作private void SyncDirectoryEntry(LdapEntry rootEntry, Org parentOrg, LdapEntry currentEntry)
{ListLdapEntry entryList currentEntry.Children(_connection);foreach (LdapEntry entry in entryList){if (entry.IsOrganizationalUnit()){Org org this.SyncOrgFromEntry(rootEntry, parentOrg, entry);this.SyncDirectoryEntry(rootEntry, org, entry);}else if (entry.IsUser()){this.SyncUserFromEntry(rootEntry, parentOrg, entry);}}
}
同步部门private Org SyncOrgFromEntry(LdapEntry rootEntry, Org parentOrg, LdapEntry entry)
{string orgId entry.Guid().ToLower();Org org this._org.GetOrgById(orgId) as Org;if (org ! null){if (entry.ContainsAttr(ou)){org.Name entry.getAttribute(ou).StringValue string.Empty;}//设置其他属性的值_org.UpdateOrg(org);return org;}org new Org{Id orgId,ParentId parentOrg.Id,};//设置其他属性的值this._org.AddOrg(org);return org;
}
同步用户private User SyncUserFromEntry(LdapEntry rootEntry, Org parentOrg, LdapEntry entry)
{string userId entry.Guid().ToLower();User user this._user.GetUserById(userId);if (user ! null){user.ParentId parentOrg.Id;//设置其他属性的值this._user.UpdateUser(user);return user;}user new User{Id userId,ParentId parentOrg.Id};//设置其他属性的值this._user.AddUser(user);return user;
}
辅助方法为了方便代码的编写和复用将一些操作提取到了扩展方法中。获取 Entry 的 GUIDpublic static string Guid(this LdapEntry entry)
{var bytes (byte[])(entry.getAttribute(objectGUID).ByteValue as object);var guid new Guid(bytes);return guid.ToString();
}
获取 Entry 的 子级public static ListLdapEntry Children(this LdapEntry entry, LdapConnection connection)
{//string filter ((objectclassuser));ListLdapEntry entryList new ListLdapEntry();LdapSearchResults lsc connection.Search(entry.DN, LdapConnection.SCOPE_ONE, objectClass*, null, false);if (lsc null) return entryList;while (lsc.HasMore()){LdapEntry nextEntry null;try{nextEntry lsc.Next();if (nextEntry.IsUser() || nextEntry.IsOrganizationalUnit()){entryList.Add(nextEntry);}}catch (LdapException e){continue;}}return entryList;
}
判断 Entry 是否为用户public static bool IsUser(this LdapEntry entry)
{return entry.ObjectClass().Contains(user);
}
判断 Entry 是否为部门public static bool IsOrganizationalUnit(this LdapEntry entry)
{return entry.ObjectClass().Contains(organizationalunit);
}
获取 Entry 的修改时间public static DateTime WhenChanged(this LdapEntry entry)
{string value entry.getAttribute(whenChanged).StringValue;if (value.Split(.).Length 1){value value.Split(.)[0];}DateTime whenChanged DateTime.ParseExact(value, yyyyMMddHHmmss, System.Globalization.CultureInfo.CurrentCulture);return whenChanged;
}
判断 Entry 中属性是否存在public static bool ContainsAttr(this LdapEntry entry, string attrName)
{LdapAttribute ldapAttribute new LdapAttribute(attrName);return entry.getAttributeSet().Contains(ldapAttribute);
}
根据名称获取 Entry 中的属性值public static string AttrStringValue(this LdapEntry entry, string attrName)
{if (!entry.ContainsAttr(attrName)){return string.Empty;}return entry.getAttribute(attrName).StringValue;
}
总结文中没有做更多文字性的介绍可以从下面链接中下载代码进行调试就很容易理解了。参考示例代码https://github.com/oec2003/StudySamples/tree/master/DotNetCoreAdDemo/DotNetCoreAdDemo祝大家国庆假期快乐
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910273.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!