使用 Novell.Directory.Ldap.NETStandard 库的 C# netcore ldap 身份验证

标签 c# authentication active-directory .net-core novell

这是我第一次使用 LDAP 和 Active Directory。我必须使用 .NetCore 创建一个 Web api,该 API 必须使用 ActiveDirectory (WindowsServer 2008 r2) 进行身份验证,我正在按照 Novell.Directory.Ldap.NETStandard 中的示例进行操作。但我无法理解必须设置参数的方式。 这是我在 ActiveDirectory 服务器中创建的用户:

/image/A7iGq.jpg

在 Novell 的示例中

if (args.Length != 5)
{
    System.Console.Out.WriteLine("Usage:   mono VerifyPassword <host name>" + " <login dn> <password> <object dn>\n" + "         <test password>");
    System.Console.Out.WriteLine("Example: mono VerifyPassword Acme.com " + "\"cn=Admin,o=Acme\" secret\n" + "         \"cn=JSmith,ou=Sales,o=Acme\" testPassword");
    System.Environment.Exit(0);
}

int ldapPort = LdapConnection.DEFAULT_PORT;
int ldapVersion = LdapConnection.Ldap_V3;
System.String ldapHost = args[0];
System.String loginDN = args[1];
System.String password = args[2];
System.String objectDN = args[3];
System.String testPassword = args[4];
LdapConnection conn = new LdapConnection();

try
{
    // connect to the server
    conn.Connect(ldapHost, ldapPort);

    // authenticate to the server
    conn.Bind(ldapVersion, loginDN, password);

    LdapAttribute attr = new LdapAttribute("userPassword", testPassword);
    bool correct = conn.Compare(objectDN, attr);

    System.Console.Out.WriteLine(correct?"The password is correct.":"The password is incorrect.\n");

    // disconnect with the server
    conn.Disconnect();
}

在 Novell 的示例中,“user”参数看起来像这样“ou=sales,o=Acme”,所以我正在尝试:

int ldapPort = LdapConnection.DEFAULT_PORT;
int ldapVersion = LdapConnection.Ldap_V3;
bool compareResults = false;
String ldapHost = "192.168.58.251";
String loginDN = @"cn=jperez";
String password1 = "Jperez123";
String dn = "mydn";
LdapConnection lc = new LdapConnection();
LdapAttribute attr = null;

try
{
    // connect to the server
    lc.Connect(ldapHost, ldapPort);
    var sdn = lc.GetSchemaDN();

    // authenticate to the server
    lc.Bind(ldapVersion, loginDN, password1);

    ...
}
catch (LdapException e)
{
    Console.WriteLine("Error: " + e.ToString());
}

但我收到此错误: LDAP:

LdapException: Invalid Credentials (49) Invalid Credentials LdapException: Server Message: 80090308: LdapErr: DSID-0C0903A8, comment: AcceptSecurityContext error, data 52e, v1db1\u0000 LdapException: Matched DN:

我还使用此函数获取 schemaDn:lc.GetSchemaDN(),它返回以下结果:CN=Aggregate,CN=Schema,CN=Configuration,DC=mydn,DC =本地

谷歌搜索后,没有比 Novell 的示例更多的 .Netcore 信息,请我需要您的帮助。

最佳答案

我也一直在研究这个问题,但遇到了同样的错误。我必须使用 Windows 域和用户名登录:

String loginDN = "DOMAIN\\jperez";
String password1 = "Jperez123";

lc.Bind(loginDN, password1);

一旦我这样做了,我就毫无问题地进入了。

关于使用 Novell.Directory.Ldap.NETStandard 库的 C# netcore ldap 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140051/

相关文章:

c# - 使 Entity Framework 实现一个接口(interface)

c# - 网格拆分器和最小宽度

c# - 重载属性 setter

c# - 删除应用程序分区抛出对象不存在的异常

windows - 添加名称中带有空格的Active Directory组会在PowerShell脚本中给出错误

c# - 未捕获异步异常

java - Spring Boot 中自定义 LdapUserDetailsS​​ervice 始终无法匹配密码

delphi - Wininet SSL 客户端验证异常

java - 如何从启用 SSO 的网络中的 Java 程序自动验证常规 Yammer 用户?

javascript - React App 的 Active Directory 插件出现 CORS 错误