以下是使用C#实现在Active Directory中添加新用户的代码示例:
using System;
using System.DirectoryServices;
class Program
{
static void Main(string[] args)
{
string username = "newUser";
string password = "password123";
string domainName = "yourdomain.com";
try
{
using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainName))
{
using (DirectoryEntry newUser = entry.Children.Add("CN=" + username, "user"))
{
newUser.Properties["samAccountName"].Value = username;
newUser.CommitChanges();
newUser.Invoke("SetPassword", new object[] { password });
newUser.CommitChanges();
}
}
Console.WriteLine("New user added successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error adding new user: " + ex.Message);
}
Console.ReadLine();
}
}
使用上述代码示例,你需要将domainName
更改为你的域名,username
和password
更改为要添加的新用户的用户名和密码。
如果你希望使用VB.NET来实现相同的功能,可以尝试以下代码示例:
Imports System
Imports System.DirectoryServices
Module Program
Sub Main()
Dim username As String = "newUser"
Dim password As String = "password123"
Dim domainName As String = "yourdomain.com"
Try
Using entry As New DirectoryEntry("LDAP://" & domainName)
Using newUser As DirectoryEntry = entry.Children.Add("CN=" & username, "user")
newUser.Properties("samAccountName").Value = username
newUser.CommitChanges()
newUser.Invoke("SetPassword", New Object() { password })
newUser.CommitChanges()
End Using
End Using
Console.WriteLine("New user added successfully.")
Catch ex As Exception
Console.WriteLine("Error adding new user: " & ex.Message)
End Try
Console.ReadLine()
End Sub
End Module
同样,你需要将domainName
更改为你的域名,username
和password
更改为要添加的新用户的用户名和密码。
希望能对你有所帮助!