当我在Windows上使用LDAP时,我可以正确获取Server ErrorMessage以抑制错误消息。但是Linux上的ServerErrorMessage结果都是空的
我在这个问题上发现了同样的问题 https://github.com/dotnet/runtime/issues/70210
这是我调用 Ldap Authenticate 的函数
public void Authenticate(string username, string password)
{
var ldapUsername = $"{_ldapDomain}\\{username}";
var ldapPassword = password;
using var connection = new LdapConnection(new LdapDirectoryIdentifier(_ldapServer, _ldapPort));
connection.AuthType = AuthType.Basic;
connection.Credential = new System.Net.NetworkCredential(ldapUsername, ldapPassword);
connection.SessionOptions.ProtocolVersion = 3;
try
{
connection.Bind();
}
catch (LdapException ex)
{
var message = ex.ErrorCode switch
{
49 => ex.ServerErrorMessage switch
{
string s when s.Contains("data 525") => "User not found. Please check your username.",
string s when s.Contains("data 52e") => "AD authentication failed. Please check your username and password.",
string s when s.Contains("data 530") => "Login not permitted at this time. Please contact your administrator.",
string s when s.Contains("data 531") => "Login not permitted from this workstation. Please contact your administrator.",
string s when s.Contains("data 532") => "Password expired. Please update your password.",
string s when s.Contains("data 533") => "Account disabled. Please contact your administrator.",
string s when s.Contains("data 534") => "Login requires a secure connection. Please ensure you are using a secure connection and try again.",
string s when s.Contains("data 701") => "Account expired. Please contact your administrator.",
string s when s.Contains("data 773") => "Password must be reset. Please update your password.",
string s when s.Contains("data 775") => "Your AD account is locked. Please try again later.",
_ => $"Authentication failed, please check your username and password, or contact support."
},
_ => "LDAP server internal error: please contact support.",
};
throw new AuthenticationException(message, ex);
}
catch
{
throw;
}
}
我用
Novell.Directory.Ldap;
更改软件包,它将在 Linux 上运行。