ASP.Net Core 2.1 Web Api 2 和 Active Directory 凭据

ASP.Net Core 2.1 Web Api 2 And Active Directory Credentials

我目前正在 ASP.Net Core 2.1 上为一组组织开发 Web Api 2 application,他们都有自己的 Active Directory Domain services

我想在 domain 上检查来自 ASP.Net Core 2.1 Web Api 2 的凭据,这将由用户指定。 我的目标是拥有一个连接到指定活动目录服务并检查凭据的功能。

如果从 active directory 获取用户信息,解决方案将更有价值,因此我可以在数据库中自动填写一些字段

我为我的项目做了同样的事情。我用过 Novell 库(也可以在 linux 下使用)Novell.Directory.Ldap

这里是文档:https://www.novell.com/documentation/developer/ldapcsharp/?page=/documentation/developer/ldapcsharp/cnet/data/bovumfi.html

为了检查用户凭据,我编写了以下代码:

using (LdapConnection testConn = new LdapConnection() { SecureSocketLayer = _useSSL })
{
    try {
        //Connect function will create a socket connection to the server - Port 389 for insecure and 3269 for secure   
        testConn.Connect(_serverName, _serverPort);

        //Bind function with null user dn and password value will perform anonymous bind to LDAP server 
        testConn.Bind(user.DistinguishedName, password);

        var res = testConn.Bound;
        return res;
    }catch (Exception ex){
        Console.WriteLine($"Authenticate: {ex.Message}");
        return false;
    }
}