DirectoryEntry是.NET Framework提供的一种用于访问Active Directory的类。它提供了访问Active Directory中的对象、属性和方法的能力,可以实现在Active Directory中创建、修改和删除对象、属性等操作。本文将深入介绍DirectoryEntry在账户启用与停用、创建账户等方面的使用方法,并附带案例说明。
一、账户启用与停用
1. 启用账户
使用DirectoryEntry启用账户时,需要获取到该账户的DirectoryEntry对象,然后设置“userAccountControl”属性的值。其中该属性有两个比特位与用户账户启用相关,可以使用OR运算将这两个比特位设置为1,使用户账户启用。
案例代码:
```
// 首先获取到指定AccountName账户的DirectoryEntry对象
DirectoryEntry deUser = new DirectoryEntry("LDAP://" + domainName + "/CN=" + accountName + "," + baseDN);
// 获取 userAccountControl 属性值
int val = (int)deUser.Properties["userAccountControl"].Value;
// 设置启用比特位
deUser.Properties["userAccountControl"].Value = val & ~0x2;
// 提交更改
deUser.CommitChanges();
```
2. 停用账户
同样,使用DirectoryEntry停用账户时,也需要获取到该账户的DirectoryEntry对象,然后设置“userAccountControl”属性的值。其中该属性有两个比特位与用户账户停用相关,可以使用AND运算将这两个比特位设置为0,使用户账户停用。
案例代码:
```
// 首先获取到指定AccountName账户的DirectoryEntry对象
DirectoryEntry deUser = new DirectoryEntry("LDAP://" + domainName + "/CN=" + accountName + "," + baseDN);
// 获取 userAccountControl 属性值
int val = (int)deUser.Properties["userAccountControl"].Value;
// 设置启用比特位
deUser.Properties["userAccountControl"].Value = val | 0x2;
// 提交更改
deUser.CommitChanges();
```
二、创建账户
使用DirectoryEntry创建账户时,需要创建一个新的DirectoryEntry对象,然后设置各个属性的值并调用CommitChanges方法提交修改。以下是一个创建账户的完整示例代码:
```
// 创建一个 DirectoryEntry 对象,指定OU为所创建用户的OU,ADsPath 为根据此处指定的内容计算出来的
DirectoryEntry deUser = new DirectoryEntry("LDAP://" + ouPath, "AdminName", "AdminPassword", AuthenticationTypes.Secure);
// 设置该账户的属性
deUser.Username = "TestUser";
deUser.Properties["displayName"].Value = "测试用户";
deUser.Properties["givenName"].Value = "测试";
deUser.Properties["sn"].Value = "用户";
deUser.Properties["samAccountName"].Value = "testuser";
deUser.Properties["userAccountControl"].Value = 0x200; // 设置账户启用
// 保存修改
deUser.CommitChanges();
```
通过以上代码可以看到,要创建一个用户,需要指定这个账户所在组织单位的DN信息(称为OU)。在代码中指定OU的方式为`DirectoryEntry deUser = new DirectoryEntry("LDAP://" + ouPath, "AdminName", "AdminPassword", AuthenticationTypes.Secure);`,其中ouPath为OU的LDAP路径,AdminName和AdminPassword为连接AD需要使用的用户名和密码。
三、案例说明
下面我们结合具体的案例,更加详细地说明DirectoryEntry账户启用与停用、账户创建等操作方法的细节。
1. 案例一:批量启用或停用账户
在某些情况下,我们需要批量启用或停用AD中的账户。比如,当我们需要关闭某个部门的域账户时,可以在AD中找到这个部门的所有账户,然后批量停用这些账户。
以下是一个示例代码,可以实现批量启用或停用指定OU下所有账户:
```
public void EnableDisabledAccounts(string ouPath, bool isEnabled)
{
// 创建 Connection 对象,需要使用 AD 连接密码认证模式
DirectoryEntry deConn = new DirectoryEntry("LDAP://" + ouPath, "AdminName", "AdminPassword", AuthenticationTypes.Secure);
// 创建一个搜索对象
DirectorySearcher ds = new DirectorySearcher(deConn);
ds.Filter = "(&(objectClass=user))"; // 只搜索用户账户
SearchResultCollection results = ds.FindAll();
// 循环遍历所有搜索结果
foreach (SearchResult result in results)
{
// 获取该账户的 DirectoryEntry 对象
DirectoryEntry deUser = result.GetDirectoryEntry();
// 获取用户账户控制属性值
int val = (int)deUser.Properties["userAccountControl"].Value;
// 启用账户
if (isEnabled)
{
deUser.Properties["userAccountControl"].Value = val & ~0x2;
}
// 停用账户
else
{
deUser.Properties["userAccountControl"].Value = val | 0x2;
}
// 提交修改
deUser.CommitChanges();
}
}
```
在以上代码中,我们首先使用DirectorySearcher进行搜索操作,获取到指定OU下的所有账户。然后,循环遍历这些账户,调用上面的启用和停用方法,并提交修改。
2. 案例二:创建账户并设置密码
在AD中创建账户时,密码为必填项,需要设置密码才能保存修改。同时,为了保证密码的安全性,在设置密码时需要使用安全遮蔽加密算法,将密码进行加密后再保存。
以下是一个示例代码,可以实现在指定OU下创建一个新账户,并设置密码:
```
public void CreateNewAccount(string ouPath, string accountName, string accountPassword)
{
// 创建 Connection 对象,需要使用 AD 连接密码认证模式
DirectoryEntry deConn = new DirectoryEntry("LDAP://" + ouPath, "AdminName", "AdminPassword", AuthenticationTypes.Secure);
// 创建一个新的 DirectoryEntry 对象,表示待创建账户
DirectoryEntry deUser = deConn.Children.Add("CN=" + accountName, "user");
deUser.Properties["displayName"].Value = "测试用户";
deUser.Properties["givenName"].Value = "测试";
deUser.Properties["sn"].Value = "用户";
deUser.Properties["samAccountName"].Value = accountName;
// 设置密码
byte[] pwdByte = Encoding.Unicode.GetBytes(accountPassword);
deUser.Invoke("SetPassword", new object[] { pwdByte });
deUser.Properties["userAccountControl"].Value = 0x200;
// 保存修改
deUser.CommitChanges();
}
```
在以上代码中,我们首先创建DirectoryEntry对象,表示待创建的新账户。然后,设置该账户各个属性的值。接下来,设置密码,并将密码加密后保存。最后,提交修改。
四、总结
本文主要介绍了DirectoryEntry在账户启用与停用、创建账户等方面的使用方法,包括启用账户、停用账户和创建账户,并附带了案例说明。需要注意的是,使用DirectoryEntry访问Active Directory时,需要使用LDAP协议,故需要指定LDAP路径等信息。同时,为保证访问Active Directory的安全性,需要使用用户名和密码进行认证。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/
发表评论 取消回复