如何通过c#代码将密码更改为用户帐户?

如何通过c#代码将密码更改为用户帐户?,c#,C#,如何通过c代码将密码更改为用户帐户?使用active directory: // Connect to Active Directory and get the DirectoryEntry object. // Note, ADPath is an Active Directory path pointing to a user. You would have created this // path by calling a GetUser() function, which searche

如何通过c代码将密码更改为用户帐户?

使用active directory:

// Connect to Active Directory and get the DirectoryEntry object.
// Note, ADPath is an Active Directory path pointing to a user. You would have created this
// path by calling a GetUser() function, which searches AD for the specified user
// and returns its DirectoryEntry object or path. See http://www.primaryobjects.com/CMS/Article61.aspx
DirectoryEntry oDE;
oDE = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure);

try
{
   // Change the password.
   oDE.Invoke("ChangePassword", new object[]{strOldPassword, strNewPassword});
} 
catch (Exception excep)
{
   Debug.WriteLine("Error changing password. Reason: " + excep.Message);
}
这里有一个在本地用户帐户中更改的示例:

另一种选择是使用互操作性和调用非托管代码:
netapi32.dll


“在管理员中运行以更改所有用户”

这里是一种更简单的方法,但是您需要从.Net 4.0中引用System.DirectoryServices.AccountManagement

namespace PasswordChanger
{
    using System;
    using System.DirectoryServices.AccountManagement;

    class Program
    {
        static void Main(string[] args)
        {
            ChangePassword("domain", "user", "oldpassword", "newpassword");
        }

        public static void ChangePassword(string domain, string userName, string oldPassword, string newPassword)
        {
            try
            {
                using (var context = new PrincipalContext(ContextType.Domain, domain))
                using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
                {
                    user.ChangePassword(oldPassword, newPassword);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

这适用于广告和本地帐户

如果要通过C#调用此API,可以使用此签名将API
NetUserChangePassword
导入C#代码:


哪个用户的帐户?域?任何应用程序?系统?可能是这个问题的翻版(取决于您要更改的密码):这是我与ho1提供的链接一起使用的,它工作得很好,然后感谢您的帮助,我如何获得ADPath?需要与ActiveDS一起使用,如果是,如何使用?抱歉,代码上的链接是隐藏的,可能堆栈溢出可以改善这一点:-)这是到AD的一般用途以及如何构建ADPath的链接:即使没有管理员权限,也非常有效ges,谢谢Paul。即使以管理员身份运行应用程序,我也遇到了一个异常AccessDenied。我最终使用了以下解决方案:
namespace PasswordChanger
{
    using System;
    using System.DirectoryServices.AccountManagement;

    class Program
    {
        static void Main(string[] args)
        {
            ChangePassword("domain", "user", "oldpassword", "newpassword");
        }

        public static void ChangePassword(string domain, string userName, string oldPassword, string newPassword)
        {
            try
            {
                using (var context = new PrincipalContext(ContextType.Domain, domain))
                using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
                {
                    user.ChangePassword(oldPassword, newPassword);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
[DllImport("netapi32.dll", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall,SetLastError=true )]
static extern uint NetUserChangePassword (

[MarshalAs(UnmanagedType.LPWStr)] string domainname,

[MarshalAs(UnmanagedType.LPWStr)] string username,

[MarshalAs(UnmanagedType.LPWStr)] string oldpassword,

[MarshalAs(UnmanagedType.LPWStr)] string newpassword);