尝试从forumsys测试基于LDAP的身份验证?

时间:2018-08-01 03:14:19

标签: c# authentication ldap

我以前没有做过任何基于LDAP的身份验证,而且以前也没有使用过任何LDAP服务器。因此,我需要一个免费的在线LDAP服务器来使用它,我已经找到了https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/

但是我的代码不起作用(或者我不确定那里的信息已经无效),authen的结果始终为false,这是我的代码:

path = "ldap.forumsys.com:389/dc=example,dc=com";
using (var pc = new PrincipalContext(ContextType.Domain, null, path))
{
  //this always returns false
  var ok = pc.ValidateCredentials("read-only-admin", "password");
}

您能使其正常工作吗?或者至少请断言那里的信息无效,在这种情况下,如果可能的话,请给我一些其他信息(从其他免费的LDAP服务器进行测试)。

3 个答案:

答案 0 :(得分:0)

似乎read-only-admin不是有效用户。尝试更换:

var ok = pc.ValidateCredentials("read-only-admin", "password");

使用

var ok = pc.ValidateCredentials("tesla", "password");

如果这不起作用,则另一个问题将在LDAP的服务器端。

一个不错的选择是设置Amazon Web Services EC2服务器(免费)并将Windows Server加载到该服务器上。这将为您提供您自己的服务器,并且您将学习如何设置LDAP服务器(这非常简单)。

答案 1 :(得分:0)

我认为服务器不是Active Directory。您可以参考this question了解如何使用C#连接到LDAP服务器。

第二次修改: 与MS人员确认。他们还建议使用LdapConnection

https://github.com/dotnet/corefx/issues/31809

编辑:

我可以使用DirectoryEntry绑定到服务器。我不确定PrincipalContext为什么不起作用,但是您可以尝试这种方式。

这是用于验证用户名和密码的示例代码。 已在带有System.DirectoryServices软件包4.5.0的.Net Core 2.1上进行了测试。

using System;
using System.DirectoryServices;

namespace LDAPTest
{
    class Program
    {
        static void Main(string[] args)
        {

            string ldapServer = "LDAP://ldap.forumsys.com:389/dc=example,dc=com";
            string userName = "cn=read-only-admin,dc=example,dc=com";
            string password = "password";

            var directoryEntry = new DirectoryEntry(ldapServer, userName, password, AuthenticationTypes.ServerBind);

            // Bind to server with admin. Real life should use a service user.
            object obj = directoryEntry.NativeObject;
            if (obj == null)
            {
                Console.WriteLine("Bind with admin failed!.");
                Environment.Exit(1);
            }
            else
            {
                Console.WriteLine("Bind with admin succeeded!");
            }

            // Search for the user first.
            DirectorySearcher searcher = new DirectorySearcher(directoryEntry);
            searcher.Filter = "(uid=riemann)";
            searcher.PropertiesToLoad.Add("*");
            SearchResult rc = searcher.FindOne();
            // First we should handle user not found.
            // To simplify, skip it and try to bind to the user.
            DirectoryEntry validator = new DirectoryEntry(ldapServer, "uid=riemann,dc=example,dc=com", password, AuthenticationTypes.ServerBind);
            if (validator.NativeObject.Equals(null))
            {
                Console.WriteLine("Cannot bind to user!");
            }
            else
            {
                Console.WriteLine("Bind with user succeeded!");
            }
        }
    }
}

参考: https://www.c-sharpcorner.com/forums/ldap-authentication2

答案 2 :(得分:0)

我也知道了这一点,并且不具备LDAP知识。

您的解决方案中的问题可能首先是,您正在使用“ ldap://”而不是“ LDAP://”,因为这是我在编写此代码时遇到的。但是我使用System.DirectoryServices库。

我针对这个宏伟的free to test LDAP server

进行了测试
var path = "LDAP://ldap.forumsys.com:389/dc=example,dc=com";
var user = $@"uid={username},dc=example,dc=com";
var pass = "password";

var directoryEntry = new DirectoryEntry(path, user, pass, AuthenticationTypes.None);

var searcher = new DirectorySearcher(directoryEntry);
searcher.PropertiesToLoad.Add("*");
var searchResult = searcher.FindOne();

但是,我不完全了解所有这些行的用途,并且寻找解决方案时,我发现了一些建议。

在路径上,“ LDAP://”字符串应位于mayus块上。

在用户中,有时您需要使用“ cn = username-admin”来验证管理员,请确保还将“身份验证”类型设置为ServerBind。

相关问题