.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域信息的更多相关文章

  1. C#在Linux下获取文件夹信息(所在磁盘总大小,使用空间,已用空间,使用率)

    1.第一种使用shell命令实现: private DiskInfo LinuxGetFolderDiskInfo(string path) { DiskInfo disk = new DiskInf ...

  2. JAVA 通过LDAP获取AD域用户及组织信息

    因为工作需求近期做过一个从客户AD域获取数据实现单点登录的功能,在此整理分享. 前提:用户可能有很多系统的情况下,为了方便账号的统一管理使用AD域验证登录,所以不需要我们的系统登录,就需要获取用户的A ...

  3. AD 域服务简介(二)- Java 获取 AD 域用户

    博客地址:http://www.moonxy.com 关于AD 域服务器搭建及其使用,请参阅:AD 域服务简介(一) - 基于 LDAP 的 AD 域服务器搭建及其使用 一.前言 先简单简单回顾上一篇 ...

  4. 什么是core dump linux下用core和gdb查询出现"段错误"的地方

    什么是core dump   linux下用core和gdb查询出现"段错误"的地方 http://blog.chinaunix.net/uid-26833883-id-31932 ...

  5. Linux 下获取LAN中指定IP的网卡的MAC(物理地址)

    // all.h// 2005/06/20,a.m. wenxy #ifndef _ALL_H#define _ALL_H #include <memory.h>#include < ...

  6. Linux下获取硬盘使用情况

    Linux下获取硬盘使用情况[总结] 1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场 ...

  7. Linux下获取和设置IP

    在Linux下获取关于IP和网关的操作:重点是对struct ifreq 的操作. 那么进入目录/usr/include/net/if.h下看查找struct ifreq结构体. /* Interfa ...

  8. C#获取AD域中计算机和用户的信息

    如果你的计算机加入了某个AD域,则可以获取该域中所有的计算机和用户的信息. 所用程序集,需要.Net Framework 4. 添加程序集引用 System.DirectoryServices.Acc ...

  9. Linux 下获取通讯IP

    #!/bin/sh # filename: get_net.sh default_route=$(ip route show) default_interface=$() address=$(ip a ...

随机推荐

  1. HTML中的Meta标签详解

    emta标签的组成:meta标签分两大部分:HTTP-EQUIV和NAME变量. HTTP-EQUIV:HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正确和精确地显 ...

  2. Centos7编译安装lnmp(nginx1.10 php7.0.2)

    我使用的是阿里云的服务器 Centos7 64位的版本 1. 连接服务器 这个是Xshell5的版本 安装好之后我们开始连接服务器 2. 安装nginx 首先安装nginx的依赖 yum instal ...

  3. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  4. sql存储过程进行条件筛选

    1.创建临时表,把存储过程结果集保存到临时表,对临时表进行筛选. Create Table #TmpTable(FieldList) Insert Into #TmpTable Exec StoreP ...

  5. 前端移动开发之rem

    前言 作为一名前端工程师,我们不仅要会PC端开发,还要会移动端开发,而且现在移动端占据主要流量,所以掌握移动端开发的技能更是必须的. 那么进行移动端的开发,什么是必须,我们想要的效果是什么? 自适应. ...

  6. php5.6 phpmystudy 版本出问题

    No input file specified的解决方法 https://jingyan.baidu.com/article/f7ff0bfccce11c2e26bb1381.html

  7. vc++ openssl 程序签名

    RSA一般有两种应用场景:   1.公钥加密.私钥解密:这是数据安全通信领域最常见情形:   2.私钥加验.公钥验签:这主要用于数字签名. 我们这里用到的是第二种情况: 这里是基于OpenSSL,首先 ...

  8. ios Block详解

    一. iOS代码块Block 1.1 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返回值, ...

  9. ABP框架系列之二十四:(Email-Sending-EF-电子邮件发送)

    Introduction Email sending is a pretty common task for almost every application. ASP.NET Boilerplate ...

  10. php判断语句

    编写代码时,可以为不同的情况执行不同的动作.可以使用判断条件语句来实现. if...else...elseif 例子一: <?php $t=date("H"); if ($t ...