LDAP validation fails when quot;User must change password on next log onquot;. Any solution?(当“用户必须在下次登录时更改密码时,LDAP 验证失败.有什么解决办法吗?)
问题描述
当设置了用户下次登录时必须更改密码"时,我遇到了用户验证问题.
I'm having trouble with a user validation when the "User must change password on next log on" is set.
这是我验证用户的方式:
Here's how I validate the user:
Boolean ValidateUser(String userName, String password)
{
try
{
var userOk = new DirectoryEntry("LDAP://<my LDAP server>",
userName,
password,
AuthenticationTypes.Secure
| AuthenticationTypes.ServerBind);
return true;
}
catch (COMException ex)
{
if (ex.ErrorCode == -2147023570) // 0x8007052E -- Wrong user or password
return false;
else
throw;
}
}
当设置必须更改密码"时,COMException 会按预期捕获,但是,ErrorCode 与密码错误时相同.
When the "must change password" is set the COMException is catched as expected, however, the ErrorCode is the same as if the password was wrong.
有谁知道如何解决这个问题?
Does anyone know how to fix this?
我需要一个返回码来表明密码正确并且用户必须更改密码.
I need a return code that tells that the password is correct AND that the user must change the password.
我不想在 C# 中实现 Kerberos,只是为了在用户必须更改密码时检查该死的标志.
I don't want to implement Kerberos in C# just to check for a damn flag when the user must change the password.
推荐答案
在网上找了很久,一些经验性的错误信息和一些通过 Win32API 的探索,我想出了一个解决方案,到目前为止有效.
After a long search on the Internet, some empirical work with error messages and some spelunking through Win32API, I've came up with a solution that, so far works.
Boolean ValidateUser(String userName, String password)
{
try
{
var user = new DirectoryEntry("LDAP://<my LDAP server>",
userName,
password);
var obj = user.NativeObject;
return true;
}
catch (DirectoryServicesCOMException ex)
{
/*
* The string " 773," was discovered empirically and it is related to the
* ERROR_PASSWORD_MUST_CHANGE = 0x773 that is returned by the LogonUser API.
*
* However this error code is not in any value field of the
* error message, therefore we need to check for the existence of
* the string in the error message.
*/
if (ex.ExtendedErrorMessage.Contains(" 773,"))
throw new UserMustChangePasswordException();
return false;
}
catch
{
throw;
}
}
这篇关于当“用户必须在下次登录时更改密码"时,LDAP 验证失败.有什么解决办法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当“用户必须在下次登录时更改密码"时,LDAP 验证失败.有什么解决办法吗?
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
