.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. 9. Palindrome Number (JAVA)

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  2. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...

  3. android 平台 java和javascript 通信问题 A WebView method was called on thread 'JavaBridge'.

      java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge ...

  4. android studio Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.ProcessException: Failed to execute aapt

    情况很奇怪 我是更新版本; 问题解决: clean project; 可能是编辑器有地方存有配置数据;

  5. [IBM][CLI Driver] SQL0270N 函数不受支持(原因码:"75")。 SQLSTATE=42997

    db2 update dbm cfg  using FEDERATED  yes 与 自动维护 (AUTO_MAINT) = ON 自动数据库备份 (AUTO_DB_BACKUP) = OFF 自动表 ...

  6. httpd安装与配置(编译安装)

    httpd简介 httpd是Apache超文本传输协议(HTTP)服务器的主程序.被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池. 通常,httpd不应该被直接调用,而应该在类 ...

  7. python 实践项目 强密码检测

    需求:写一个函数,它使用正则表达式,确保传入的口令字符串是强口令.强口令的定义是:长度不少于 8 个字符,同时包含大写和小写字符,至少有一位数字.你可能需要用多个正则表达式来测试该字符串,以保证它的强 ...

  8. 小白的CTF学习之路2——二进制数据基础与运算(上)

    今天的第二更,被我拖到了傍晚,嘿嘿,二进制这方面让本就数学不好的我很头疼,所以研究了一段时间 在学习之前我们先了解几个问题: 32位是几个字节? 01011100对于十进制是多少? 00001111向 ...

  9. 10. Halloween 万圣节

    10. Halloween 万圣节 (1) On October the 31st,across Britain and the USA,thousands of children are dress ...

  10. POJ - 3984 迷宫问题 dfs解法

    #include<stdio.h> #include<string.h> #include<stack> #include<algorithm> usi ...