日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dotNET Core 中怎样操作 AD?

發布時間:2023/12/4 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dotNET Core 中怎样操作 AD? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

做企業應用開發難免會跟 AD 打交道,在之前的 dotNET FrameWork 時代,通常使用 System.DirectoryServices 的相關類來操作 AD ,在 dotNET Core 中沒有這個命名空間,在張善友大佬的推薦下,知道了 Novell.Directory.Ldap。

操作 AD,通常有兩種常見的場景:

  • 將第三方數據源數據(人事系統)同步到 AD 中

  • 將 AD 數據同步到自己的數據庫中

本文將介紹在 dotNET Core 中使用 Novell.Directory.Ldap 將 AD 數據同步到數據庫的操作。

環境

  • dotNET Core:2.1

  • Novell.Directory.Ldap.NETStandard2_0:3.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;List<LdapEntry>?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; }

連接 AD

public?bool?ADConnect() {_adHost?=?"192.168.16.160";string?adAdminUserName?=?"administrator";string?adAdminPassword?=?"123456";_adPaths?=new?string[]?{?"OU=oec2003,DC=COM,DC=cn"?};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) {List<LdapEntry>?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 的 GUID

public?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?List<LdapEntry>?Children(this?LdapEntry?entry,?LdapConnection?connection) {//string?filter?=?"(&(objectclass=user))";List<LdapEntry>?entryList?=?new?List<LdapEntry>();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

祝大家國慶假期快樂!

總結

以上是生活随笔為你收集整理的dotNET Core 中怎样操作 AD?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。