1. 引言

最近一段时间设计和实现公司内部的基于OAuth2.0的统一身份认证中心,经梳理,公司部分自研系统可以使用OAuth2.0的方式进行身份认证,还有一部分系统无源代码,未开放接口,使用windows用户作为系统的用户。面对这种情况,同时为实现一个中心一键开关账户的功能,对于无源码、未开放接口、使用windows用户作为系统用户的系统,单独开发接口程序,有数据库的直接操作数据库将账号密码同步至数据库中;对于使用windows用户作为系统用户的系统,则在其部署的服务器上部署webapi接口,同步管理用户和密码。本文主要介绍的是C#对windows本地用户的新增、删除、修改密码功能以及列出所有本地用户的功能。

2. Active Directory与DirectoryEntry 类

C#管理windows用户,在百度上搜索到c#操作windows本地账户这样一篇文章,主要是通过导入Netapi32.dll文件实现对windows本地账户的管理。而在之前有做过使用DirectoryEntry 类修改本地用户密码的功能,经搜索,DirectoryEntry 类可封装 Active Directory 域服务层次结构中的节点或对象,也能实现对用户的新增、删除以及其他功能。

Active Directory

活动目录(Active Directory)是面向Windows Standard Server、Windows Enterprise Server以及 Windows Datacenter Server的目录服务。(Active Directory不能运行在Windows Web Server上,但是可以通过它对运行Windows Web Server的计算机进行管理。)Active Directory存储了有关网络对象的信息,并且让管理员和用户能够轻松地查找和使用这些信息。Active Directory使用了一种结构化的数据存储方式,并以此作为基础对目录信息进行合乎逻辑的分层组织。

可参考: Active Directory的基本概念

DirectoryEntry 类

DirectoryEntry类位于System.DirectoryServices表空间下,可封装 Active Directory 域服务层次结构中的节点或对象。DirectoryEntry类使用 Active Directory Services Interfaces (ADSI) 技术。 ADSI 是 Microsoft 为灵活的工具提供用于处理各种网络提供程序的接口的集合。 ADSI 使管理员能够定位和管理网络上的资源相对容易地,而不考虑网络的大小。

可参考:DirectoryEntry 类

3. 管理本地用户

using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Web; namespace OAuthClient.Common
{
public class WindowsUser : IUser
{
private static readonly string PATH = "WinNT://" + Environment.MachineName; /// <summary>
/// 获取所有用户
/// </summary>
/// <returns></returns>
public List<User> GetAllUser()
{
List<User> list = new List<User>();
using (DirectoryEntry deRoot = new DirectoryEntry(PATH))
{
if (deRoot.Children != null)
{
foreach (DirectoryEntry de in deRoot.Children)
{
if (de.SchemaClassName == "User" ||
de.SchemaClassName == "Computer" ||
de.SchemaClassName == "Domain")
{
User user = new User()
{
name = de.Name,
fullname = de.Properties["FullName"].Value.ToString()
};
list.Add(user);
}
}
}
return list;
}
} /// <summary>
/// 新增用户
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public string AddUser(User user)
{
try
{
using (DirectoryEntry deRoot = new DirectoryEntry(PATH))
{
using (DirectoryEntry de = deRoot.Children.Add(user.name, "User"))
{
de.Properties["FullName"].Add(user.fullname); //用户全称
de.Invoke("SetPassword", user.password); //用户密码
de.Invoke("Put", "Description", user.description);//用户详细描述
de.Invoke("Put", "UserFlags", 66049); //密码永不过期
de.CommitChanges();
return "OK";
}
}
}
catch (Exception ex)
{
return ex.Message;
}
} /// <summary>
/// 移除用户
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string RemoveUser(string name)
{
try
{
using (DirectoryEntry deRoot = new DirectoryEntry(PATH))
{
using (DirectoryEntry user = deRoot.Children.Find(name, "User"))
{
if (user != null)
dir.Children.Remove(user);
return "OK";
}
}
}
catch (Exception ex)
{
return ex.Message;
}
} /// <summary>
/// 修改用户密码
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public string ChangePassword(User user)
{
try
{
using (DirectoryEntry deRoot = new DirectoryEntry(PATH))
{
using (DirectoryEntry de = dir.Children.Find(user.name, "User"))
{
de.Invoke("SetPassword", new object[] { user.password });
de.CommitChanges();
return "OK";
}
}
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}

4. webapi下注意项

  1. 在webapi下,如果使用DirectoryEntry类,需添加Microsoft.Web.Infrastructure的引用。
  2. 在web.config中,需增加如下的配置节,否则会报拒绝访问的错误。
 <system.web>
<identity impersonate="true" />
</system.web>

使用webapi实现windows本地用户管理的更多相关文章

  1. windows本地用户及组的区别

    Administrators(超级管理员组) 用来管理注册用户或者具有一定权限的其他管理员,维护网站的运行. Administrators中的用户对计算机/域有不受限制的完全访问权,分配给该组的默认权 ...

  2. puppet aix之自动化用户管理

    一.    用户组的管理 (一)   Puppet组管理特性 1.   manages_aix_lam 用来管理AIX的LAM(Loadable Authentication Module)系统. 2 ...

  3. linux虚拟机中FTP本地用户模式配置流程

    1.首先在自己虚拟机中安装vsftpd服务,可以先去yum中下载(当然你要有本地yum仓库) 输入命令: yum  install  vsftpd 下载完成之后打开vsftpd服务 输入命令:syst ...

  4. 管理Windows Server 2008本地用户和组

    下面介绍Windows Server 2008本地用户和组的管理包括创建用户.删除用户.重设密码.将用户添加到组.普通用户跟管理员的区别 .用户配置文件包括桌面上文件,桌面背景,桌面上图标,IE设置, ...

  5. Windows Server 2008 R2入门之用户管理

    一.用户账户概述: ”用户”是计算机的使用者在计算机系统中的身份映射,不同的用户身份拥有不同的权限,每个用户包含一个名称和一个密码: 在Windows中,每个用户帐户有一个唯一的安全标识符(Secur ...

  6. Windows Server 2008 用户管理

    默认用户和组 默认用户 默认只有来宾用户(Guest)和管理员(Administrator) 默认组 创建账户 图形界面创建用户 创建用户选项解析 对于公司新员工,分配给他的电脑,应该让其有一定的自主 ...

  7. windows本地破解用户口令

    实验所属系列:操作系统安全 实验对象: 本科/专科信息安全专业 相关课程及专业:信息网络安全概论.计算机网络 实验时数(学分):2学时 实验类别:实践实验类 实验目的 1.了解Windows2000/ ...

  8. DOS命令行(6)——Windows网络状态及用户管理

    ipconfig --查看计算机中适配器的TCP/IP配置信息 命令格式: ipconfig [/allcompartments] [/? | /all | /renew [adapter] | /r ...

  9. Windows Server2003本地用户的批量导入和导出(转)

    AD域环境的用户导入和导出 Windows server 2003 批量导入用户---CSVDE 在新搭建的域环境中,有许多的域帐号需要导入,可以采用csvde命令来导入域用户:新建一个txt文本文件 ...

随机推荐

  1. 【最大权闭合子图/最小割】BZOJ3438-小M的作物【待填】

    [题目大意] 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子有1个(就是可以种一棵作物)(用1...n编号),现在,第i种作物种植在A中种植 ...

  2. 调用上一个页面的js方法

    点击商品分类,弹出下框 点击确定,将选中的类别的name和唯一的code返回到上个页面 function save(){ var ids = getIdSelections(); jp.get(&qu ...

  3. Hadoop学习入门

    1.hadoop相关术语 HDFS: Hadoop分布式文件系统(HDFS,Hadoop Distributed Filesystem) MapReduce: NameNode: DataNode: ...

  4. delphi 获取USB口拔出和插入的状态

    unit USBDeviceNotify;//USB Device arrival or removeinterface uses  Windows, Messages, SysUtils, Clas ...

  5. eth0: ERROR while getting interface flags: No such device

    出现这个问题有两种原因: 虚拟机设置中没有添加对应的网卡 更改了虚拟机中网卡的MAC,但是Debian 的缓存中将eth0与上次的MAC对应 解决方法: 这里仅就第二种问题提出解决方案: 删除/etc ...

  6. 从配置websocket理解nginx

    原文地址:http://www.niu12.com/article/2 今天由于写了一个简单的基于h5 websoceket的聊天室,再本地都是好好了.     但是上到服务器后就发现无法行的通, 查 ...

  7. HTMLTestRunner美化

    https://www.cnblogs.com/findyou/p/6925733.html 参考这个,美化的不错,进入了汉化,及加入了一些样式,

  8. artDialog弹出框使用

    配置参数 名称 类型 默认值 描述 内容 title String '消息' 标题内容 content String 'loading..' 消息内容.1.如果传入的是HTMLElement类型,如果 ...

  9. Oracle中分页查询语句的写法

    要动态的变化分页查询的条件,比如pageNow 这个变量表示的是当前是第几页, oracle分页有通用写法,假设一页5行 select * from ( select t.*,rownum rn fr ...

  10. [Spring boot] Configuring and Accessing a Data Source

    We need our data persistence with configuring our datasouce: In application.properties: spring.h2.co ...