返回顶部

收藏

C#修改电脑密码,重置windows密码DirectoryEntry实现

更多

C#修改电脑密码,重置windows密码DirectoryEntry实现

///C#修改电脑密码方法如下
/// <summary>
/// 更改Windows帐户密码
/// </summary>
/// <param name="username"></param>
/// <param name="oldPwd"></param>
/// <param name="newPwd"></param>
public static void ChangeWinUserPasswd(string username, string oldPwd, string newPwd)
{
    try
    {
        DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
        DirectoryEntry user = localMachine.Children.Find(username, "user");
        object[] password = new object[] { oldPwd, newPwd };
        object ret = user.Invoke("ChangePassword", password);
        user.CommitChanges();
        localMachine.Close();
        user.Close();
    }
    catch (Exception)
    {

    }
} 

///C# 重置电脑密码
/// <summary>
/// 重置指定用户的密码
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">新密码</param>
public static void ResetUserPassword(string userName, string password)
{
    string _Path = "WinNT://" + Environment.MachineName;

    DirectoryEntry machine = new DirectoryEntry(_Path); //获得计算机实例
    DirectoryEntry user = machine.Children.Find(userName, "User"); //找得用户
    if (user != null)
    {
        user.Invoke("SetPassword", password); //用户密码
        user.CommitChanges();
    } 
}
//该片段来自于http://outofmemory.cn

标签:c#,.net,安全

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2014-04-29 17:39:54C#校验给定的ip地址是否合法 by 流云诸葛
  2. 2014-06-14 11:37:30计算哈希值 by 牛哥
  3. 2014-05-01 19:01:53C#获取指定字符串的MD5和SHA*加密代码 by 掌心雷
  4. 2014-05-04 19:54:20验证邮箱 by 掌心雷
  5. 2014-05-05 19:07:00C# Password Hashing by 格格巫2012
  6. 2014-05-08 21:16:18标准 XXTEA 加密算法 by Jlinhai
  7. 2014-11-01 11:15:02C#得到随机安全码(哈希加密)的封装类 by Jlinhai
  8. 2014-11-11 20:48:32md5Hash by Jlinhai
  9. 2014-04-28 18:35:30C#封装的简化xml操作的类 by 牛哥
  10. 2014-07-14 11:38:16C# CRC校验类 by did0602
  11. 2014-05-05 20:33:51vs测试汇编 by bargain

发表评论