.net core在Linux下获取AD域信息
.net core在Linux下获取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"]?[].ToString());
dic.Add("mail",result.Properties["mail"]?[].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(, 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": ,
"MailSuffix": "@xxx.com",
"Attrs": "displayname|mail|sn",
"SearchBase": "DC=xxx,DC=com",
"UserRole": "User"
},
.net core在Linux下获取AD域信息的更多相关文章
- C#在Linux下获取文件夹信息(所在磁盘总大小,使用空间,已用空间,使用率)
1.第一种使用shell命令实现: private DiskInfo LinuxGetFolderDiskInfo(string path) { DiskInfo disk = new DiskInf ...
- JAVA 通过LDAP获取AD域用户及组织信息
因为工作需求近期做过一个从客户AD域获取数据实现单点登录的功能,在此整理分享. 前提:用户可能有很多系统的情况下,为了方便账号的统一管理使用AD域验证登录,所以不需要我们的系统登录,就需要获取用户的A ...
- AD 域服务简介(二)- Java 获取 AD 域用户
博客地址:http://www.moonxy.com 关于AD 域服务器搭建及其使用,请参阅:AD 域服务简介(一) - 基于 LDAP 的 AD 域服务器搭建及其使用 一.前言 先简单简单回顾上一篇 ...
- 什么是core dump linux下用core和gdb查询出现"段错误"的地方
什么是core dump linux下用core和gdb查询出现"段错误"的地方 http://blog.chinaunix.net/uid-26833883-id-31932 ...
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
// all.h// 2005/06/20,a.m. wenxy #ifndef _ALL_H#define _ALL_H #include <memory.h>#include < ...
- Linux下获取硬盘使用情况
Linux下获取硬盘使用情况[总结] 1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场 ...
- Linux下获取和设置IP
在Linux下获取关于IP和网关的操作:重点是对struct ifreq 的操作. 那么进入目录/usr/include/net/if.h下看查找struct ifreq结构体. /* Interfa ...
- C#获取AD域中计算机和用户的信息
如果你的计算机加入了某个AD域,则可以获取该域中所有的计算机和用户的信息. 所用程序集,需要.Net Framework 4. 添加程序集引用 System.DirectoryServices.Acc ...
- Linux 下获取通讯IP
#!/bin/sh # filename: get_net.sh default_route=$(ip route show) default_interface=$() address=$(ip a ...
随机推荐
- ACM-ICPC 2018 南京赛区网络预赛 J.sum(欧拉筛)
题目来源:https://nanti.jisuanke.com/t/A1956 题意:找一个数拆成无平方因子的组合数,然后求前缀和. 解题思路:我们可以把某个数分解质因数,如果某个数可以分解出三个相同 ...
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...
- Web前端(整理不好,自己未学)
1.公司招聘信息 (1)小公司 (2)腾讯 ①社会招聘 ②校园招聘 (3)百度 ①社会招聘 ②实习 ③校园招聘 2.岗位要求 开发经验,良好的编程习惯,学习能力,至少二个项目开发设计,具备需求功能模块 ...
- ios 在使用 SDWebimage UIButton setBackgroundimage
解决方法 : 有法为此在 SDWebImage: SDWebImage / SDWebImage / UIButton+WebCache.h 导入此文件在您的类: #import <SDWebI ...
- c++11 线程池学习笔记 (一) 任务队列
学习内容来自一下地址 http://www.cnblogs.com/qicosmos/p/4772486.html github https://github.com/qicosmos/cosmos ...
- Linux 6上使用UDEV绑定共享存储
1.硬盘的查看方式 [root@cl6-11gr2-rac1 ~]# ls -ltr /dev/sd* brw-rw----. 1 root disk 8, 48 8月 16 13:34 /dev/s ...
- JS基础-数组的常用方法-冒泡排序
1.数组 1.关联数组 以数字作为元素下标的数组,就是索引数组. 以字符串作为元素下标的数组,就是关联数组. 2.js的关联数组 ex:在php中 $array=[& ...
- tomcat是否有必要配置环境变量(摘)
之前发表了一篇关于如何安装和配置Tomcat的文章,而最近在开发项目的时候总是报错.后来被公司的大神问了一句:是谁告诉你Tomcat是需要配置环境变量的? 作为新手的我瞬间整个人都不好了!于是偷偷百度 ...
- flask 学习
标题 操作 09-2-sqlalchemy数据库查询 (2019-01-18 23:30) 编辑 09-1-数据库扩展包flask-sqlalchemy (2019-01-18 17:53) 编辑 0 ...
- js后加版本号
页面引入两个js文件 <script charset="utf-8" src="mjs/randomimage.js?v=01291"></s ...