gpt4 book ai didi

c# - 我想设置 "Password Must Change at Next Login"标志

转载 作者:行者123 更新时间:2023-12-02 11:17:04 24 4
gpt4 key购买 nike

在我的应用程序中,我正在执行用户可以从我的应用程序控制他/她的本地 Windows 用户帐户的操作,即创建用户、设置/删除密码、更改密码以及可以从我的应用程序调用密码过期策略。现在,此时,我需要弄清楚如果用户想在下次登录时更改密码,那么会发生什么。正如许多论坛和博客对此所说的那样,我进行了相应的编码:

调用密码在下次登录时过期

 public bool InvokePasswordExpiredPolicy()
{
try
{
string path = GetDirectoryPath();
string attribute = "PasswordExpired";
DirectoryEntry de = new DirectoryEntry(path);
de.RefreshCache(new string[] { attribute });
if(de.Properties.Contains("PasswordExpired"))
de.Properties[attribute].Value = 1;
de.CommitChanges();
return true;
}
catch (Exception)
{
return false;
}
}

导致密码在下次登录时过期。重置标志

public bool ProvokePasswordExpiredPolicy()
{
try
{
string path = GetDirectoryPath();
string attribute = "PasswordExpired";
DirectoryEntry de = new DirectoryEntry(path);
de.RefreshCache(new string[] { attribute });
de.Properties[attribute].Value = -1;
de.CommitChanges();
return true;
}
catch (Exception)
{
return false;
}
}

检查相关标志是否设置

public bool isPasswordPolicyInvoked()
{
try
{
string path = GetDirectoryPath();
string attribute = "PasswordExpired";
DirectoryEntry de = new DirectoryEntry(path);
de.RefreshCache(new string[] { attribute });
int value = Convert.ToInt32(de.Properties[attribute].Value);

if (value == 1)
return true;
else
return false;
}
catch (Exception)
{
return false;
}
}

我使用 WinNT 来获取目录路径,而不是 LDAP。我使用以下方法来获取目录路径。

private String GetDirectoryPath()
{
String uName = this.userName;
String mName = this.userMachine;

String directoryPath = "WinNT://" + mName + "/" + uName;

return directoryPath;
}

我有什么遗漏的吗?帮帮我吧。

注意: 首先,我使用 pwdLastSet 属性设置为 0(表示打开)和 -1(表示关闭),这会引发异常“找不到目录属性” in Property Cache”,后来我发现WinNT不支持这个属性,而是支持PasswordExpired,它需要设置为1才能设置标志。我就是这么做的。

最佳答案

如何使用 System.DirectoryServices.AccountManagement 来代替,在这种情况下您可以调用以下代码:

UserPrincipal.Current.ExpirePasswordNow();

关于c# - 我想设置 "Password Must Change at Next Login"标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11218136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com