使用webapi实现windows本地用户管理
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使用了一种结构化的数据存储方式,并以此作为基础对目录信息进行合乎逻辑的分层组织。
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下注意项
- 在webapi下,如果使用DirectoryEntry类,需添加
Microsoft.Web.Infrastructure的引用。 - 在web.config中,需增加如下的配置节,否则会报拒绝访问的错误。
<system.web>
<identity impersonate="true" />
</system.web>
使用webapi实现windows本地用户管理的更多相关文章
- windows本地用户及组的区别
Administrators(超级管理员组) 用来管理注册用户或者具有一定权限的其他管理员,维护网站的运行. Administrators中的用户对计算机/域有不受限制的完全访问权,分配给该组的默认权 ...
- puppet aix之自动化用户管理
一. 用户组的管理 (一) Puppet组管理特性 1. manages_aix_lam 用来管理AIX的LAM(Loadable Authentication Module)系统. 2 ...
- linux虚拟机中FTP本地用户模式配置流程
1.首先在自己虚拟机中安装vsftpd服务,可以先去yum中下载(当然你要有本地yum仓库) 输入命令: yum install vsftpd 下载完成之后打开vsftpd服务 输入命令:syst ...
- 管理Windows Server 2008本地用户和组
下面介绍Windows Server 2008本地用户和组的管理包括创建用户.删除用户.重设密码.将用户添加到组.普通用户跟管理员的区别 .用户配置文件包括桌面上文件,桌面背景,桌面上图标,IE设置, ...
- Windows Server 2008 R2入门之用户管理
一.用户账户概述: ”用户”是计算机的使用者在计算机系统中的身份映射,不同的用户身份拥有不同的权限,每个用户包含一个名称和一个密码: 在Windows中,每个用户帐户有一个唯一的安全标识符(Secur ...
- Windows Server 2008 用户管理
默认用户和组 默认用户 默认只有来宾用户(Guest)和管理员(Administrator) 默认组 创建账户 图形界面创建用户 创建用户选项解析 对于公司新员工,分配给他的电脑,应该让其有一定的自主 ...
- windows本地破解用户口令
实验所属系列:操作系统安全 实验对象: 本科/专科信息安全专业 相关课程及专业:信息网络安全概论.计算机网络 实验时数(学分):2学时 实验类别:实践实验类 实验目的 1.了解Windows2000/ ...
- DOS命令行(6)——Windows网络状态及用户管理
ipconfig --查看计算机中适配器的TCP/IP配置信息 命令格式: ipconfig [/allcompartments] [/? | /all | /renew [adapter] | /r ...
- Windows Server2003本地用户的批量导入和导出(转)
AD域环境的用户导入和导出 Windows server 2003 批量导入用户---CSVDE 在新搭建的域环境中,有许多的域帐号需要导入,可以采用csvde命令来导入域用户:新建一个txt文本文件 ...
随机推荐
- 苹果iOS系统下检查第三方APP是否安装及跳转启动
在iOS系统,使用Url Scheme框架在APP间互相跳转和传递数据,本文只介绍如果检测和跳转. Url Scheme框架 如果你想知道ios设备中是否安装QQ这个软件,我们可以通过一个简单方法判断 ...
- Enable a SQL Server Trace Flag Globally on Linux
https://www.mssqltips.com/sql-server-tip-category/226/sql-server-on-linux// Microsoft has recently r ...
- docker常用命令 状态图
http://blog.csdn.net/permike/article/details/51879578
- Debian安装VirtualBox增强工具
切换到root用户: apt-get install build-essential 或者 apt-get install gcc make apt-get install dkms apt-get ...
- ThreadPoolExecutor 的三种提交任务方式
学习内容: ExecutorService线程池的应用... 1.如何创建线程池... 2.调用线程池的方法,获取线程执行完毕后的结果... 3.关闭线程... 首先我们先了解一下到底什么是线程池 ...
- 怎样使用libcurl获取隐藏了文件后缀的url网络文件类型
CURLINFO_CONTENT_TYPE CURL: Get Returned Content Mime Type 例如 :以下代码可以查询出天地图的tile图像类型为jpg "http: ...
- Vue生命周期各阶段发生的事情
首先,参考之前一篇vue生命周期的总结:Vue生命周期总结 接下来我们来分析下官方文档经典流程图,每个阶段到底发生了什么事情. 1.在beforeCreate和created钩子函数之间的生命周期 在 ...
- [Linux]屏幕输出控制
专门的术语叫做ANSI Escape sequences(ANSI Escape codes),题目并不恰当,与其说是屏幕输出控制,不如说是通过bash在兼容VT100的终端上进行输出. 主要有以下类 ...
- 蓝鲸安装Agent
1. APPO 所在机器(在 app 运行所在机器) 必须能通过 ssh 登陆到 Agent 机器2. Agent 所在机器可以访问到 zk 的端口3. 支持 Linux/Windows/AIX 操作 ...
- orchard project 本地化
http://orchardproject.net/localization 本地化 果园的本地化管理是托管在一个外部服务( Crowdin), 的项目.公众和贡献是受欢迎的! 如何做出贡献 注册上 ...