从MVC3应用程序更改AD密码

时间:2012-08-15 10:29:25

标签: asp.net-mvc-3 active-directory wif adfs2.0

我正在使用WIF和ADFS 2.0构建MVC3 Web应用程序。我想要做的是为我的用户提供更改密码功能,他们将从这个Web应用程序更改他们的AD密码。由于我处于开发阶段,我必须能够从我的开发计算机(外部域)更改AD密码。稍后,权限将委派给运行具有足够访问权限的服务的用户。

我想做这个角色,而不输入用户名和密码,我似乎无法找到任何指向正确方向的资源。

有什么建议吗?

1 个答案:

答案 0 :(得分:4)

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");
    }
}
相关问题