Novell.Directory.Ldap.NETStandard是一个在.NET Core中,既支持Windows平台,又支持Linux平台,进行Windows AD域操作的Nuget包。

首先我们新建一个.NET Core控制台项目:NetCoreLdap,并下载如下Nuget包:

Novell.Directory.Ldap.NETStandard

然后我们新建一个.NET Core类LDAPUtil,用来对域账号进行验证:

using System;
using Novell.Directory.Ldap; namespace NetCoreLdap
{
/// <summary>
/// LDAP工具类
/// </summary>
public static class LDAPUtil
{
public static string Domain = "apac";//域名称
public static string Host = "apac.contoso.com";//域服务器地址
public static string BaseDC = "DC=apac,DC=contoso,DC=com";//根据上面的域服务器地址,每个点拆分为一个DC,例如上面的apac.contoso.com,拆分后就是DC=apac,DC=contoso,DC=com
public static int Port = ;//域服务器端口,一般默认就是389
public static string DomainAdminUser = "admin";//域管理员账号用户名,如果只是验证登录用户,不对域做修改,可以就是登录用户名
public static string DomainAdminPassword = "1qaz!QAZ";//域管理员账号密码,如果只是验证登录用户,不对域做修改,可以就是登录用户的密码 /// <summary>
/// 验证域用户的账号和密码
/// </summary>
/// <param name="username">域用户的账号</param>
/// <param name="password">域用户的密码</param>
/// <returns>true验证成功,false验证失败</returns>
public static bool Validate(string username, string password)
{
try
{
using (var conn = new LdapConnection())
{
conn.Connect(Host, Port);
conn.Bind(Domain + "\\" + DomainAdminUser, DomainAdminPassword);//这里用户名或密码错误会抛出异常LdapException var entities =
conn.Search(BaseDC, LdapConnection.ScopeSub,
$"sAMAccountName={username}",//注意一个多的空格都不能打,否则查不出来
new string[] { "sAMAccountName", "cn", "mail" }, false); string userDn = null;
while (entities.HasMore())
{
var entity = entities.Next();
var sAMAccountName = entity.GetAttribute("sAMAccountName")?.StringValue;
var cn = entity.GetAttribute("cn")?.StringValue;
var mail = entity.GetAttribute("mail")?.StringValue; Console.WriteLine($"User name : {sAMAccountName}");//james
Console.WriteLine($"User full name : {cn}");//James, Clark [james]
Console.WriteLine($"User mail address : {mail}");//james@contoso.com //If you need to Case insensitive, please modify the below code.
if (sAMAccountName != null && sAMAccountName == username)
{
userDn = entity.Dn;
break;
}
}
if (string.IsNullOrWhiteSpace(userDn)) return false;
conn.Bind(userDn, password);//这里用户名或密码错误会抛出异常LdapException
// LdapAttribute passwordAttr = new LdapAttribute("userPassword", password);
// var compareResult = conn.Compare(userDn, passwordAttr);
conn.Disconnect();
return true;
}
}
catch (LdapException ldapEx)
{
string message = ldapEx.Message; return false;
}
catch (Exception)
{
return false;
}
} }
}

然后在.NET Core控制台项目的Main方法中调用LDAPUtil.Validate方法,来验证一个AD账户:

using System;

namespace NetCoreLdap
{
class Program
{
static void Main(string[] args)
{
string username = "james";
string password = "2wsx@WSX"; var loginFlag = LDAPUtil.Validate(username, password); if(loginFlag)
{
Console.WriteLine("User validate successfully!");
}
else
{
Console.WriteLine("User validate unsuccessfully!");
} Console.ReadLine();
}
}
}

本例源代码

使用Novell.Directory.Ldap.NETStandard在.NET Core中验证AD域账号的更多相关文章

  1. novell.directory.ldap获取邮箱活动目录

    在windows系统上可以使用下列方法来查找所有的员工邮箱和员工组: StringDictionary ReturnArray = new StringDictionary(); Dictionary ...

  2. 第十四节:Asp.Net Core 中的跨域解决方案(Cors、jsonp改造、chrome配置)

    一. 整体说明 1. 说在前面的话 早在前面的章节中,就详细介绍了.Net FrameWork版本下MVC和WebApi的跨域解决方案,详见:https://www.cnblogs.com/yaope ...

  3. ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS)

    ylbtech-ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS) 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. ...

  4. 在 ASP.NET Core 中启用跨域请求(CORS)

    本文介绍如何在 ASP.NET Core 的应用程序中启用 CORS. 浏览器安全可以防止网页向其他域发送请求,而不是为网页提供服务. 此限制称为相同源策略. 同一源策略可防止恶意站点读取另一个站点中 ...

  5. ASP.NET Core AD 域登录

    在选择AD登录时,其实可以直接选择 Windows 授权,不过因为有些网站需要的是LDAP获取信息进行授权,而非直接依赖Web Server自带的Windows 授权功能. 当然如果使用的是Azure ...

  6. ASP.NET Core AD 域登录 (转载)

    在选择AD登录时,其实可以直接选择 Windows 授权,不过因为有些网站需要的是LDAP获取信息进行授权,而非直接依赖Web Server自带的Windows 授权功能. 当然如果使用的是Azure ...

  7. .net core在Linux下获取AD域信息

    .net core在Linux下获取AD域信息 .net Core 2.1.4 .net core现在System.DirectoryServices只支持Windows平台下使用. 参考: http ...

  8. Ldap实现AD域认证

    1.java Ldap基础类 package com.common; import java.io.FileInputStream; import java.io.IOException; impor ...

  9. AD 域服务简介(一)- 基于 LDAP 的 AD 域服务器搭建及其使用(转)

    一.前言 1.1 AD 域服务 什么是目录(directory)呢? 日常生活中使用的电话薄内记录着亲朋好友的姓名.电话与地址等数据,它就是 telephone directory(电话目录):计算机 ...

随机推荐

  1. Js利用Canvas实现图片压缩

    最近做的APP项目涉及到手机拍照上传图片,因为手机拍照的图片通常都比较大,所以上传的时候就会很慢.为此,需要对图片进行压缩处理来优化上传功能.以下是具体实现: /* * 图片压缩 * img 原始图片 ...

  2. RecyclerView源码解析 - 分割线

    猜想:   既然考虑了分割线,那么子View在测量时候肯定要去考虑分割线留出的位置    直接measureChild()方法 猜想: 分割线会调用绘制的方法 onDraw()

  3. Android--自定义弹出框-自定义dialog

    项目要用到弹出框,还要和苹果的样式一样(Android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以. 废话不多说了,直接上代码 1.先看布局文件 <?xml vers ...

  4. 8.2,常用模块介绍:sys,os,time,random

    sys: 介绍:主要包含涉及python编译器与系统交互的函数. 常用函数: import sys print(sys.argv)#本文件名,已经运行该程序时的参数 #[如在命令窗口中python3 ...

  5. 简单了解Tomcat与OSGi的类加载器架构

    前言: 本次博客主要是对Tomcat与OSGi的类加载器架构,所以就需要对tomcat.OSGi以及类加载机制有所了解 类加载可以在http://www.cnblogs.com/ghoster/p/7 ...

  6. ssh中文手册

    ssh-keygen 中文手册 sshd_config 中文手册 sshd 中文手册

  7. orcle 如何快速插入百万千万条数据

    有时候做实验测试数据用到大量数据时可以用以下方法插入: 方法一:使用xmltable create table bqh8 as select rownum as id from xmltable('1 ...

  8. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  9. C++进阶书籍(转)

    推荐的阅读顺序:level 1从<<essential c++>>开始,短小精悍,可以对c++能进一步了解其特性以<<c++ primer>>作字典和课 ...

  10. android开发之一如何升级SDK

    看了很多文章,都没有成功,下面这篇才是正解,学海无涯苦作舟. Fetching https://dl-ssl.google.com/android/repository/addons_list-2.x ...