分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

.net core获取AD域信息

发布时间:2023-09-06 01:44责任编辑:彭小芳关键词:暂无标签

.net core获取AD域信息

.net Core 2.1.4

.net core现在System.DirectoryServices只支持Windows平台下使用。

参考:

https://github.com/dotnet/standard/pull/444

https://github.com/dotnet/corefx/issues/2089

private Dictionary<string,string> AuthenticateActiveDirectory(string username, string password){ ???Dictionary<string, string> dic = new Dictionary<string, string>(); ???DirectoryEntry entry = new DirectoryEntry(_appConfiguration["LDAP:DE"], username, password); ???try ???{ ???????DirectorySearcher search = new DirectorySearcher(entry); ????????search.Filter = $"(SAMAccountName={username})"; ?????????SearchResult result = search.FindOne(); ???????if (result != null) ???????{ ???????????dic.Add("state","true"); ???????????dic.Add("displayname", result.Properties["displayname"]?[0].ToString()); ???????????dic.Add("mail",result.Properties["mail"]?[0].ToString()); ???????} ???} ???catch (Exception ex) ???{ ???????dic.Add("state", "false"); ???????dic.Add("errMsg",ex.Message); ???} ???return dic;}

Novell.Directory.Ldap

Novell.Directory.Ldap支持.net core2 Linux环境。

public Dictionary<string, string> LdapAuthenticate(string username, string password){ ???Dictionary<string, string> dic = new Dictionary<string, string>(); ???var ldapHost = _appConfiguration["LDAP:Host"]; ???var ldapPort = _appConfiguration.GetValue<int>("LDAP:Port"); ???var mailSuffix = _appConfiguration["LDAP:MailSuffix"]; ???var searchBase = _appConfiguration["LDAP:SearchBase"]; ???var loginDN = username; ???var sAMAccountName = username; ???if (username.Contains(mailSuffix)) ???????sAMAccountName = username.Substring(0, username.IndexOf(mailSuffix)); ???else ???????loginDN = $"{username}{mailSuffix}"; ???????var searchFilter = $"(sAMAccountName={sAMAccountName})"; ???var attrs = _appConfiguration["LDAP:Attrs"].Split(‘|‘); ???try ???{ ???????var conn = new LdapConnection(); ???????conn.Connect(ldapHost, ldapPort); ???????conn.Bind(loginDN, password); ???????var lsc = conn.Search(searchBase, LdapConnection.SCOPE_SUB, searchFilter, attrs, false); ???????while (lsc.hasMore()) ???????{ ???????????LdapEntry nextEntry = null; ???????????try ???????????{ ???????????????nextEntry = lsc.next(); ???????????} ???????????catch (LdapException ex) ???????????{ ???????????????Logger.Debug(ex.ToString(), ex); ???????????????continue; ???????????} ???????????var attributeSet = nextEntry.getAttributeSet(); ???????????var ienum = attributeSet.GetEnumerator(); ???????????while (ienum.MoveNext()) ???????????{ ???????????????var attribute = (LdapAttribute)ienum.Current; ???????????????var attributeName = attribute.Name.ToLower(); ???????????????var attributeVal = attribute.StringValue; ???????????????if (attrs.Contains(attributeName)) ???????????????{ ???????????????????dic.Add(attributeName, attributeVal); ???????????????} ???????????} ???????????dic.Add("state", "true"); ???????} ???????conn.Disconnect(); ???} ???catch (Exception ex) ???{ ???????dic.Add("state", "false"); ???????dic.Add("errMsg", ex.Message); ???????Logger.Debug(ex.ToString(), ex); ???} ???return dic;}

以上配置信息如下:

 ?"LDAP": { ???"_comment": "域帐号登录配置", ???"DE": "LDAP://xxx.com", ???"Host": "xx.xx.xx.xx", ???"Port": 389, ???"MailSuffix": "@xxx.com", ???"Attrs": "displayname|mail|sn", ???"SearchBase": "DC=xxx,DC=com", ???"UserRole": "User" ?},

.net core获取AD域信息

原文地址:https://www.cnblogs.com/ddrsql/p/8516226.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved