通过C#将用户创建到活动目录 [英] create user into active directory by C#
本文介绍了通过C#将用户创建到活动目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
我的代码问题。当我试图运行我的代码以在AD中创建用户时显示错误。
这里是错误
登录失败未知用户名或密码错误。
这里是代码
hi
I've problem with my code . there is an error shown when i'm trying to run my code to create a user into AD.
here is the error
logon failure unknown username or bad password.
here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
namespace AD
{
public partial class NewUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//try
//{
DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://IP/CN=Users,DC=TEST,DC=COM", "UserName", "Password");
if (adUserFolder.SchemaEntry.Name == "container")
{
DirectoryEntry newUser = adUserFolder.Children.Add("CN=" + username.Text, "User");
if (DirectoryEntry.Exists(newUser.Path))
{
Label1.Text = "user name is exists";
}
else
{
newUser.Properties["samAccountName"].Value = accountname.Text;
newUser.Properties["givenName"].Value = firstname.Text;
newUser.Properties["sn"].Value = lastname.Text;
newUser.Properties["initials"].Value = initials.Text;
newUser.Properties["siplayName"].Value = displayname.Text;
newUser.Properties["physicalDeliveryOfficeName"].Value = officename.Text;
newUser.Properties["telephoneNumber"].Value = phone.Text;
newUser.Properties["mail"].Value = Email.Text;
newUser.CommitChanges();
newUser.Invoke("setpassword", password.Text);
newUser.Properties["userAccountControl"].Value = 0x0200;
newUser.CommitChanges();
Label1.Text = "done!";
}
}
//}
//catch
//{
// Label1.Text = "wrong!";
//}
}
}
}
我确定登录信息正确无误。
and i'm sure that the logon info are correct.
推荐答案
使用以下内容连接到AD:
Connect to AD using something like this:
var username = "your username";
var password = "your password";
var domain = "your domain";
var ctx = new PrincipalContext(ContextType.Domain, domain, username, password);
然后执行操作PrincipalContext对象。
祝你好运
Espen Harlinn
and then perform the operations using the PrincipalContext object.
Best regards
Espen Harlinn
这篇关于通过C#将用户创建到活动目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文