• 改写:C#里根据Windows本地用户组查询组成员,以及修改密码(System.DirectoryServices.Protocols使用"WinNT://"方式)


    大作原文: http://www.cnblogs.com/songrui/archive/2009/08/05/1539770.html

    重构如下:

    using System;
    using System.DirectoryServices; //这两个using一定要写上去
    using System.DirectoryServices.Protocols;
    using System.Collections;
    using System.Collections.Generic;//要在reference里添加System.DirectoryServices.dll和System.DirectoryServices.Protocols.dll

    namespace MyTool
    {
    /// <summary>
    /// 主要包括下面两个方法,其他方法也可以用,主要为辅助主方法用
    /// UpdatePassWord(string var) 修改本地密码的方法
    /// GetUserInfoArrayList(string var) 根据用户组,查询本地包含用户HashTable(含名称、全名、描述)的数组
    /// </summary>
    public class WindowsGroupUser
    {
    public class UserInfo
    {
    public string UserName { get; set; }
    public string FullName { get; set; }
    public string Description { get; set; }
    }
    /// <summary>
    /// 修改本地密码的方法
    /// </summary>
    /// <param name="intputPwd">输入的新密码</param>
    /// <returns>成功返回"success",失败返回exception</returns>
    public static string UpdatePassWord(string intputPwd)
    {
    try
    {
    DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
    DirectoryEntry user = AD.Children.Find("test", "User");
    user.Invoke("SetPassword", new object[] { intputPwd });
    return "success";
    }
    catch (Exception ex)
    {
    return ex.ToString();
    }
    }
    /// <summary>
    /// 根据本地用户组获得组里的用户名数组
    /// </summary>
    /// <param name="localGroup">本地用户组</param>
    /// <returns>用户名数组</returns>
    public static List<string> GetUsersArrayList(DirectoryEntry directoryEntry)
    {
    var arrUsers = new List<string>();
    try
    {
    foreach (object member in (IEnumerable)directoryEntry.Invoke("Members"))
    {
    DirectoryEntry dirmem = new DirectoryEntry(member);
    arrUsers.Add(dirmem.Name);
    }
    return arrUsers;
    }
    catch { return arrUsers; }

    }
    /// <summary>
    /// 获得每个单独的用户信息
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <param name="directoryEntry">目录入口</param>
    /// <returns>单独用户信息的HashTable</returns>
    public static UserInfo GetSingleUserInfo(string userName, string localGroup)
    {

    try
    {
    DirectoryEntry group = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
    try
    {
    System.DirectoryServices.DirectoryEntry user = group.Children.Find(userName, "User");
    string FullName = Convert.ToString(user.Invoke("Get", new object[] { "FullName" }));
    string Description = Convert.ToString(user.Invoke("Get", new object[] { "Description" }));

    return new UserInfo() { UserName = userName, FullName = FullName, Description = Description };
    }
    catch { };

    }
    catch { }
    return null;
    }
    /// <summary>
    /// 根据用户组,查询本地包含用户HashTable(含名称、全名、描述)的数组
    /// </summary>
    /// <param name="localGroup">用户组名称</param>
    /// <returns>包含用户HashTable(含名称、全名、描述)的数组</returns>
    public static List<UserInfo> GetUserInfoArrayList(string localGroup)
    {
    var arr = new List<UserInfo>();//al返回HASHTABLE数组用

    try
    {
    DirectoryEntry group = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + localGroup + ",group");

    foreach (string user in GetUsersArrayList(group))
    {
    arr.Add(GetSingleUserInfo(user, localGroup));
    }
    }
    catch (Exception ex)
    {
    string errMsg = ex.ToString();
    }
    return arr;
    }
    }
    }



    alarm   作者:NewSea     出处:http://newsea.cnblogs.com/    QQ,MSN:iamnewsea@hotmail.com

      如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。
  • 相关阅读:
    Verilog HDL刷题笔记(06)(Circuit-Combinational Logic-Arithmetic Circuit)
    Verilog HDL刷题笔记(05)(Circuit-Combinational Logic-Multiplexers)
    Verilog HDL刷题笔记(04)(Circuit-Combinational Logic-Basic Gates)
    Verilog HDL刷题笔记(03)
    Verilog HDL刷题笔记(02)
    Verilog HDL刷题笔记(01)
    某点评 手机验证码自动登录
    线程锁实现多线程读取mongo 数据库库
    Python mongo 快速读取
    正方教務管理系統RSA 加密
  • 原文地址:https://www.cnblogs.com/newsea/p/2429025.html
Copyright © 2020-2023  润新知