创建AD用户在C# [英] create AD-User in c#

查看:305
本文介绍了创建AD用户在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新的AD用户与此code:

I am trying to create a new AD-User with this code:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Domain", "ou=some_ou, dc=Mydomain");
UserPrincipal user = new UserPrincipal(ctx, account, passwd, true);
user.GivenName = Givenname;
user.Surname = Surname;
user.DisplayName = Displayname;
user.UserPrincipalName = account + "@Domain";                
user.Save();

用户无需创建错误的。但我也必须设置如地址等特性,因此code继续:

The User is created without error. But I also have to set properties like Address etc, so the code continues with:

string distname = user.DistinguishedName;
DirectoryEntry duser = new DirectoryEntry(distname);
try
{
    duser.Properties["company"].Value = "Company";
}
catch (Exception e)
{
}

现在我正在

错误:System.Exception._COMPlusException code -532459699

Error: System.Exception._COMPlusExceptionCode -532459699

字符串 DISTNAME 显示正确的专有名称。

The string distname shows the correct distinguished name.

推荐答案

我看到你正在使用的UserPrincipal凭据,

I see you are using credentials in the UserPrincipal,

难道你忘了创建的DirectoryEntry时要使用他们呢? 此外,您还需要添加LDAP://你的服务器名之前

Did you forgot to use them when creating your DirectoryEntry? Also, you need to add "LDAP://" before you server name

试着这么做:

DirectoryEntry duser = new DirectoryEntry("LDAP://" + distname);
duser.Username = account;
duser.Password = passwd;
duser.AuthenticationType = AuthenticationTypes.Secure; 

这篇关于创建AD用户在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆