.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. 20175126《Java程序设计》第一周学习总结

    # 学号 20175126 <Java程序设计>第一周学习总结   ## 教材学习内容总结   - 1.安装了WINDOS系统的JDK,并学会了利用JDK编写并编译JAVA程序的基本方法. ...

  2. ATM取款机数据库设计

    创建文件夹    USE master GO EXEC xp_cmdshell 'mkdir d:\bank', NO_OUTPUT 建库   --检验数据库是否存在,如果为真,删除此数据库--   ...

  3. maven发布jar包到私服

    1.setting.xml配置 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi=&quo ...

  4. python的配置

    1.下载python https://jingyan.baidu.com/article/0bc808fc42dfab1bd485b99f.html 2.转载:https://www.cnblogs. ...

  5. 虚拟机下 centos7 无法连接网络

    [root@localhost ~]# cd /etc/sysconfig/network-scripts [root@localhost network-scripts]# ls ifcfg-ens ...

  6. AI for VS ,美团创新之处分析

    微软在2017中发布了VS Tools for AI,旨在提升用户对于深度学习的需求体验.AI组件可以让我们迅速构建和训练深度学习的Project,其功能主要有开发,调试和部署深度学习和人工智能的解决 ...

  7. .NET MVC 学习笔记(一)— 新建MVC工程

    一..NET MVC 学习笔记(一)—— 新建MVC工程 接触MVC有段时间了,一直想找机会整理一下,可是限于文笔太差,所以一直迟迟羞于下手,想到最近做过的MVC项目也有一些了,花点时间整理一下方便以 ...

  8. Django _VIEW视图_源码分析

    Django _VIEW视图: 1. 点击as_view方法. 第二步: as_view () 为VIEW 类里定义的,到时候我们定义业务逻辑的类就继承这个VIEW类. view方法内返回的是disp ...

  9. Python PIL 库的应用

    PIL (Python Image Library) 库是Python 语言的一个第三方库,PIL库支持图像存储.显示和处理,能够处理几乎所有格式的图片. 一.PIL库简介 1. PIL库主要有2个方 ...

  10. Yii2 三层设计模式:SQL Command、Query builder、Active Record(ORM)

    用Yii2也有一段时间了,发现Yii2 Framework对Database的操作有非常良好的结构和弹性. 接下来介绍三种数据库操作方式. SQL Command Level: // Get DB c ...