在我的应用程序中,我正在做的事情是用户可以从我的应用程序控制他/她的本地 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(表示关闭)抛出异常“在属性缓存中找不到目录属性”,后来我发现 WinNT 不支持这个属性而是它支持PasswordExpired,它需要为 1 才能设置标志。这就是我所做的。