http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5819|回复: 1

[C#语言基础] C#修改电脑密码,重置电脑密码DirectoryEntry实现

[复制链接]
发表于 2012-10-15 15:06:05 | 显示全部楼层 |阅读模式
C#修改电脑密码方法如下
[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#重置电脑密码如下
[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();
            }
        }

下面提供一下类大家可以参考一下1.创建windows帐户
[C#] 纯文本查看 复制代码
/// <summary>
/// 创建Windows帐户
/// </summary>
/// <param name="pathname"></param>
/// <returns></returns>
public static void CreateLocalUser(string username, string password, string description)
{
    DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
    var newUser = localMachine.Children.Add(username, "user");
    newUser.Invoke("SetPassword", new object[] { password });
    newUser.Invoke("Put", new object[] { "Description", description });
    newUser.CommitChanges();
    localMachine.Close();
    newUser.Close();
}

2.更改Windows帐户密码

/// <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)
{
    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();
}

3.判断Windows用户是否存在

/// <summary>
/// 判断Windows用户是否存在
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public static bool ExistWinUser(string username)
{
    try
    {
        using (DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
        {
            var user = localMachine.Children.Find(username, "user");
            return user != null;
        }
    }
    catch
    {
        return false;
    }
}

4.删除Windows用户

/// <summary>
/// 删除Windows用户
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public static bool DeleteWinUser(string username)
{
    try
    {
        using (DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
        {
            //删除存在用户
            var delUser = localMachine.Children.Find(username, "user");
            if (delUser != null)
            {
                localMachine.Children.Remove(delUser);
            }
        }
        return true;
    }
    catch
    {
        return false;
    }
}



5.启用/禁用windows帐户

/// <summary>
/// 启用/禁用windows帐户
/// </summary>
/// <param name="username"></param>
public static void Disable(string username, bool isDisable)
{
    var userDn = "WinNT://" + Environment.MachineName + "/" + username + ",user";
    DirectoryEntry user = new DirectoryEntry(userDn);
    user.InvokeSet("AccountDisabled", isDisable);
    user.CommitChanges();
    user.Close();
}

操作windows帐户的方法的诀窍在于通过DirectoryEntry 实例调用Invoke,InvokeGet,InvokeSet这三个方法。此三个方法可以对对本机 Active Directory 对象调用方法。操作win帐户的Active Directory 对象就是IADsUser接口。DirectoryEntry 实例通过调用Invoke方法调用IADsUser接口的方法,如上面修改Windows帐户密码就是通过调用IADsUser接口的“ChangePassword”方法;通过InvokeGet和InvokeSet方法调用IADsUser接口的属性,如上面的启用/禁用windows帐户,调用IADsUser接口的“AccountDisabled”属性



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-8-11 11:42:49 | 显示全部楼层
受教了,学习中……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-15 20:29

© 2014-2021

快速回复 返回顶部 返回列表