【问题标题】:Change AD password from MVC3 application从 MVC3 应用程序更改 AD 密码
【发布时间】:2012-08-11 15:46:19
【问题描述】:
我正在使用 WIF 和 ADFS 2.0 构建一个 MVC3 Web 应用程序。我想做的是为我的用户提供更改密码功能,他们将从此 Web 应用程序更改他们的 AD 密码。由于我处于开发阶段,我必须能够从我的开发计算机(域外)更改 AD 密码。稍后,权限将委派给运行该服务且具有足够访问权限的用户。
我想根据这个角色做这个角色,而不需要输入用户名和密码,而且我似乎找不到任何可以为我指明正确方向的资源。
有什么建议吗?
【问题讨论】:
标签:
asp.net-mvc-3
active-directory
wif
adfs2.0
【解决方案1】:
WIF 或 AD FS 中没有任何特定的内容可用于更改用户密码。您必须使用 System.DirectoryServices 命名空间中提供的标准 AD 功能。
以下是一些在 AD 中更改密码的示例代码:
internal UserPrincipal GetUser(string userName)
{
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, "YourADController",
"YourADContainer",
"ADAdminUser", "ADAdminPassword");
UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, userName);
return user;
}
internal void ResetPassword(string userName, string newPassword)
{
try
{
//
// Update normal AD attributes
//
UserPrincipal user = GetUser(userName);
user.SetPassword(newPassword);
}
catch (PasswordException)
{
throw new Exception("Password does not meet complexity requirements");
}
}
internal void SetPassword(string userName, string oldPassword, string newPassword)
{
try
{
//
// Update normal AD attributes
//
UserPrincipal user = GetUser(userName);
user.ChangePassword(oldPassword, newPassword);
}
catch (PasswordException)
{
throw new Exception("Password does not meet complexity requirements");
}
}