1. 引言
最近一段時間設(shè)計(jì)和實(shí)現(xiàn)公司內(nèi)部的基于OAuth2.0的統(tǒng)一身份認(rèn)證中心,經(jīng)梳理,公司部分自研系統(tǒng)可以使用OAuth2.0的方式進(jìn)行身份認(rèn)證伍伤,還有一部分系統(tǒng)無源代碼,未開放接口,使用windows用戶作為系統(tǒng)的用戶耘眨。面對這種情況,同時為實(shí)現(xiàn)一個中心一鍵開關(guān)賬戶的功能境肾,對于無源碼剔难、未開放接口、使用windows用戶作為系統(tǒng)用戶的系統(tǒng)奥喻,單獨(dú)開發(fā)接口程序偶宫,有數(shù)據(jù)庫的直接操作數(shù)據(jù)庫將賬號密碼同步至數(shù)據(jù)庫中;對于使用windows用戶作為系統(tǒng)用戶的系統(tǒng)环鲤,則在其部署的服務(wù)器上部署webapi接口纯趋,同步管理用戶和密碼。本文主要介紹的是C#對windows本地用戶的新增、刪除吵冒、修改密碼功能以及列出所有本地用戶的功能纯命。
2. Active Directory與DirectoryEntry 類
C#管理windows用戶,在百度上搜索到c#操作windows本地賬戶這樣一篇文章痹栖,主要是通過導(dǎo)入Netapi32.dll
文件實(shí)現(xiàn)對windows本地賬戶的管理亿汞。而在之前有做過使用DirectoryEntry 類修改本地用戶密碼的功能,經(jīng)搜索揪阿,DirectoryEntry 類可封裝 Active Directory 域服務(wù)層次結(jié)構(gòu)中的節(jié)點(diǎn)或?qū)ο罅莆遥材軐?shí)現(xiàn)對用戶的新增、刪除以及其他功能南捂。
Active Directory
活動目錄(Active Directory)是面向Windows Standard Server吴裤、Windows Enterprise Server以及 Windows Datacenter Server的目錄服務(wù)。(Active Directory不能運(yùn)行在Windows Web Server上黑毅,但是可以通過它對運(yùn)行Windows Web Server的計(jì)算機(jī)進(jìn)行管理嚼摩。)Active Directory存儲了有關(guān)網(wǎng)絡(luò)對象的信息,并且讓管理員和用戶能夠輕松地查找和使用這些信息矿瘦。Active Directory使用了一種結(jié)構(gòu)化的數(shù)據(jù)存儲方式枕面,并以此作為基礎(chǔ)對目錄信息進(jìn)行合乎邏輯的分層組織。
DirectoryEntry 類
DirectoryEntry類位于System.DirectoryServices表空間下缚去,可封裝 Active Directory 域服務(wù)層次結(jié)構(gòu)中的節(jié)點(diǎn)或?qū)ο蟪泵亍irectoryEntry類使用 Active Directory Services Interfaces (ADSI) 技術(shù)。 ADSI 是 Microsoft 為靈活的工具提供用于處理各種網(wǎng)絡(luò)提供程序的接口的集合易结。 ADSI 使管理員能夠定位和管理網(wǎng)絡(luò)上的資源相對容易地枕荞,而不考慮網(wǎng)絡(luò)的大小。
可參考: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);//用戶詳細(xì)描述
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下注意項(xiàng)
- 在webapi下搞动,如果使用DirectoryEntry類躏精,需添加
Microsoft.Web.Infrastructure
的引用。 - 在web.config中鹦肿,需增加如下的配置節(jié)矗烛,否則會報(bào)拒絕訪問的錯誤。
<system.web>
<identity impersonate="true" />
</system.web>