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. Swift中计算String的长度

        extension String {     var length: Int { return countElements(self) }  // Swift 1.1 } extension ...

  2. canvas的一些问题记录

    canvas当被设置width时,原来绘制的内容,将会清空.

  3. SQLSERVER LATCH WINDBG

    https://mssqlwiki.com/2012/09/07/latch-timeout-and-sql-server-latch/

  4. ftrace

    下面是一些关于ftrace的博文: ftrace 简介(IBM) 使用 ftrace 调试 Linux 内核 第 1 部分 第 2 部分 第 3 部分 Android系统性能调优工具介绍

  5. ORA-12537:TNS连接已关闭

      安装完11i的VIS版本后,客户端连接数据时会报“ORA-12537:TNS连接已关闭”,在网上找到以下解决办法: 今天在远程客户端配置EBS数据库连接的时候发生“ORA-12537:TNS连接已 ...

  6. Automatic Diagnostic Repository

    转载自 http://www.eygle.com/archives/2007/08/11g_auto_diag_repository.html#comments Oracle Database 11g ...

  7. hdu 1863 畅通project

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  8. 自建Saltstack的repo软件源仓库

    因为Saltstack自己的repo源是在国外,在国内服务器yum安装Saltstack的时候下载软件包就非常慢,很多情况下还经常下载失败,其实软件包总大小只有10M左右,如果这样安装多台minion ...

  9. ubuntu中apt使用以及centos中yum的使用

    centos和ubuntu是两大linux主流阵营 在centos中下载安装软件的方式 rpm rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各 ...

  10. RequireJS全面讲解

    异步模块定义(AMD)  谈起RequireJS,你无法绕过提及JavaScript模块是什么,以及AMD是什么. JavaScript模块只是遵循SRP(Single Responsibility  ...