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. POJ 3728 The merchant(并查集+DFS)

    [题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...

  2. 【记忆化搜索】bzoj1055 [HAOI2008]玩具取名

    f(l,r,c)表示sl...sr能否被合成字符c. #include<cstdio> #include<cstring> using namespace std; int m ...

  3. Echarts无数据时只显示文字不显示动画

    只需要在option中加入如下代码即可: noDataLoadingOption: {                        text: '暂无数据',                     ...

  4. 一个简单的WeakList的实现

    有的时候,我们会使用到WeakList,它和传统的List不同的是,保存的是对象的弱应用,在WeakList中存储的对象会被GC回收,在一些和UI相关的编程的地方会用到它(弱事件,窗体的通知订阅等). ...

  5. 配置.net程序集搜索路径

    默认情况下,.net程序对外部程序集dll的搜索路径是exe文件所在的目录,虽然这种方式没有什么太多不好的地方,但是当我们引用外部程序集较多的时候显得非常杂乱.一种比较常用的解决方式是通过配置在app ...

  6. 再谈 Promise

    读完这篇文章,预计会消耗你 40 分钟的时间. Ajax 出现的时候,刮来了一阵异步之风,现在 Nodejs 火爆,又一阵异步狂风刮了过来.需求是越来越苛刻,用户对性能的要求也是越来越高,随之而来的是 ...

  7. nginx 实现 ajax 跨域请求

    原文:http://www.nginx.cn/4314.html   AJAX从一个域请求另一个域会有跨域的问题.那么如何在nginx上实现ajax跨域请求呢?要在nginx上启用跨域请求,需要添加a ...

  8. Linq 简明教程

    一个简单的实例 static void Main(string[] args) { string[] names = { "Alonso", "Zheng", ...

  9. crontab 写入文件目录

    一.crontab 目录 [root@next-cloud-server etc]# cd /var/spool/cron/ [root@next-cloud-server cron]# ls roo ...

  10. 字典对象的 Pythonic 用法(上篇:转载)

    转载:https://mp.weixin.qq.com/s?timestamp=1498528588&src=3&ver=1&signature=DfFeOFPXy44ObCM ...