当我在 Windows 上使用 LDAP 时,我可以正确获取 Server ErrorMessage 来抑制错误消息。但在 Linux 上 ServerErrorMessage 结果全为空,而且我在此上发现了同样的问题...
我在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;
}
}