使用 C# 在 Active Directory 中的特定 OU 中创建用户

2024-01-18

非常感谢 marc_s 提供了我上一期中的以下代码示例在 Active Directory 中创建用户时出现 C# 错误 https://stackoverflow.com/questions/8684059/creating-user-in-active-directory-with-c-sharp-errors

public static string ldapPath = "LDAP://OU=Domain Users,DC=contoso,DC=com";
public static string CreateUserAccount(string userName, string userPassword)
{
    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "contoso.com",ldapPath);

    // create a user principal object
    UserPrincipal user = new UserPrincipal(ctx, userName, userPassword, true);

    // assign some properties to the user principal
    user.GivenName = "User";
    user.Surname = "One";

    // force the user to change password at next logon
    user.ExpirePasswordNow();

    // save the user to the directory
    user.Save();

    return user.SamAccountName;
}

现在我正在尝试将用户帐户添加到特定的 OU 中。将 ldapPath 保留在 PrimaryContext 错误中

System.DirectoryServices.AccountManagement.PrincipalOperationException: Unknown error (0x80005000) ---> System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_SchemaEntry()
   at System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de)
   at System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options)
   at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
   --- End of inner exception stack trace ---
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
   at System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t)
   at System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse()
   at System.DirectoryServices.AccountManagement.Principal.set_SamAccountName(String value)
   at System.DirectoryServices.AccountManagement.UserPrincipal..ctor(PrincipalContext context, String samAccountName, String password, Boolean enabled)
   at ADINtegrationTest.ActiveDirectory.CreateUserAccount(String userName, String userPassword) in D:\_data\ADINtegrationTest\ADINtegrationTest\ActiveDirectoryUtils.cs:line 20
   at ADINtegrationTest.Form1.Form1_Load(Object sender, EventArgs e) in D:\_data\ADINtegrationTest\ADINtegrationTest\Form1.cs:line 32

如果我删除 ldapPath,它可以正常工作,但会将用户帐户放入用户 OU 中。我还尝试了 LDAP://contoso.com/OU=Domain Users,DC=contoso,DC=com 等 ldapPath,但不起作用。


我认为您的主要上下文构造函数的 LDAP 路径有点错误 - 如果您查看我给您的 MSDN 文章的链接,您会看到:

// create a context for a domain called Fabrikam pointed
// to the TechWriters OU and using default credentials
PrincipalContext domainContext = 
   new PrincipalContext(ContextType.Domain, "Fabrikam", "ou=TechWriters,dc=fabrikam,dc=com");

我也不确定你是否可以使用互联网风格的域名contoso.com- 尝试使用 NetBIOS 风格CONTOSO反而。所以对于你的情况,你应该尝试:

public static string ldapPath = "OU=Domain Users,DC=contoso,DC=com";

public static string CreateUserAccount(string userName, string userPassword)
{
     // set up domain context
     PrincipalContext ctx = 
         new PrincipalContext(ContextType.Domain, "CONTOSO", ldapPath);

    // create a user principal object
    .... (and the rest of your code as you had it)
}

那对你有用吗??

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 C# 在 Active Directory 中的特定 OU 中创建用户 的相关文章

  • Powershell 将变量传递给带有通配符的过滤器

    我在这方面遇到了困难 我知道这可能是一个简单的语法问题 我不知道如何将此变量传递到代码块中并正确确认它 user Some Person 这正如我所期望的那样 get aduser filter Samaccountname eq user
  • Active Directory 是否支持事务?

    简单的问题 但我在任何地方都找不到答案 Active Directory 是否支持事务 换句话说 以下更改是否会回滚 因为我没有调用scope Complete using var scope new TransactionScope Di
  • 使用 ASP.NET 获取 Active Directory 信息,无需用户名和密码

    我正在尝试从 ASP NET Web 应用程序获取本地网络上用户的 Active Directory 信息 Web 应用程序运行在IIS在本地网络上 我想要的 当用户登录网站时 他们可以从 Active Directory 中看到自己的名字
  • 将 byte[] 或对象转换为 GUID

    我为对象数据类型分配了一些值 例如 object objData dc GetDirectoryEntry Properties objectGUID Value 该对象返回如下值 byte 16 0 145 1 104 2 117 3 1
  • Active Directory“-approx”过滤器运算符如何工作?

    查看 AD Cmdlet 时 Filter今天早些时候 我遇到了一个我以前从未见过的操作员 approx 我可以在几个博客和一些 TechNet 文章中找到提到此运算符 但我没有找到任何解释此运算符的用途的信息 除了 大约等于 的使用定义之
  • Azure 应用服务 Active Directory 身份验证访问被拒绝

    我们有一个 Web 应用程序 正在从 Azure 经典云服务过渡到应用服务 Web 应用程序 经典云服务位于包含我们的域控制器 常规 AD 而不是 Azure AD 的 vnet 上 应用程序服务使用 VNET 集成 因此它连接到我们的 v
  • 如何按名字和姓氏排序,然后按 SamAccountName 排序,其中并非所有姓名都有名字和姓氏?

    目前 我有以下内容 来自 LDAP Get context based on currently logged on user PrincipalContext domainContext new PrincipalContext Cont
  • Active Directory UserPrincipal.Current.GetGroups() 返回本地组而不是 Web 服务器上的组

    以下内容在我的本地开发盒上效果很好 但是 当我将其移动到网络服务器时 它失败了 甚至不会记录错误 public static List
  • 设置密码非常慢

    使用以下方式与活动目录通信时 我们遇到性能问题System DirectoryServices DirectoryEntry Invoke SetPassword new object password 有时需要15秒 进行调用的服务正在同
  • 使用通配符的 Active Directory 查询性能较差

    我正在用 C 编写一个方法 该方法应该查询 Active Directory 并查找具有以下格式的显示名称的所有用户和组 显示名称 带有前导和尾随通配符的通配符搜索 该方法将用于自动完成字段 问题是我编写的方法的性能非常差 尝试查询 AD
  • Gerrit 和 Active Directory

    我正在尝试设置 Gerrit 以使用我们的公司 Active Directory 进行身份验证 我知道很多人都设法让它发挥作用 但它对我来说不起作用 如果我运行一个ldapsearch命令如下我得到了正确的结果 所以我知道我的搜索字符串是正
  • 仅从 AD 获取计算机名称

    我是 Power Shell 新手 正在测试一些命令和想法 我坚持认为应该很简单的事情 我想将 AD 中计算机对象的名称提取到文件中 到目前为止我正在尝试的方法是这样的 computers Get ADComputer Filter For
  • 根据 AD 组成员身份限制对 WPF 视图的访问

    我们有一个 WPF 应用程序 我们希望根据用户的 AD 组成员身份限制对应用程序的访问 我们可以将其作为每个视图的属性 或者作为用户启动应用程序时的检查吗 任何代码示例将不胜感激 在 NET 3 5 及更高版本上执行此操作的最简单方法是使用
  • 验证域用户凭据

    我需要一种方法来验证 Windows 上本机 C 的用户 密码对 输入的是用户名和密码 用户可以是 DOMAIN user 格式 基本上我需要编写一个函数 如果用户 密码是有效的本地帐户 则返回 true 第1部分 如果用户 密码在给定的域
  • 如何在复杂环境中使用 FQDN 获取 NETBIOS 域名

    从完全限定的 Active Directory 域名获取 NETBIOS 域名有时是一项繁琐的任务 我找到了一个很好的答案here https stackoverflow com a 13814584 1027551 然而 在具有多个林的环
  • Powershell:根据属性过滤属性

    我对 PowerShell 的自学经验有限 所以这可能是一些基本的东西 但我似乎无法正确理解 我在 Active Directory 中 需要提取电子邮件地址不以 SamAccountName 开头的用户列表 因此 如果您的登录名是 jdo
  • 如何针对 Nancy 中的 Active Directory 进行身份验证?

    这是一篇过时的文章 但是http msdn microsoft com en us library ff650308 aspx paght000026 step3 http msdn microsoft com en us library
  • 使用用户身份验证的 SQL 数据库与 Active Directory 的连接字符串

    我正在连接到 Azure 服务器上的数据库 其中数据库位于不同的服务器上 并且通过选择选项 Active Directory 和用户凭据选项建立连接 它在 SQL Server 中工作 但我需要一个连接字符串来连接数据库 有什么建议如何去做
  • UserPrincipal 相当于 DirectoryEntry.Invoke?

    我正在更新与应用程序中的 AD 交互的代码 当前代码使用ActiveDs接口 我正在更改代码以使用 System DirectoryServices AccountManagement 命名空间 我们的应用程序允许用户存储密码提示 这存储在
  • 我需要从 ldap 找出禁用的用户

    我正在尝试使用 ldapsearch 实用程序查明用户是否在 ldap 中被禁用 但到目前为止我还没有成功 这就是我到目前为止所得到的 ldapsearch h hostname D Service Account b basedn sAM

随机推荐