Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:51:35+00:00 2026-05-27T20:51:35+00:00

Many thanks to marc_s for the following code sample, from my previous issue Creating

  • 0

Many thanks to marc_s for the following code sample, from my previous issue Creating user in Active Directory with C# 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;
}

Now I’m trying to get the user account into a specific OU. Keeping the ldapPath in the PrincipalContext errors

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

And if i remove the ldapPath, it works fine, but throws the user account into the Users OU. I also tried the ldapPath like LDAP://contoso.com/OU=Domain Users,DC=contoso,DC=com, which didn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:51:36+00:00Added an answer on May 27, 2026 at 8:51 pm

    I think you have the LDAP path a bit wrong for your principal context constructor – if you check out the link to the MSDN article I gave you, you would see:

    // 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");
    

    I’m also not sure if you can use the internet-style domain name contoso.com – try using the NetBIOS style CONTOSO instead. So in your case, you should try:

    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)
    }
    

    Does that work for you??

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which returns results back from a database where columnName
Many thanks for the advice you have given me thus far. Using testbenches is
edit: many thanks for all the answers. Here are the results after applying the
First of all many thanks for clicking into my question. I have a few
Can anyone recommend any good resources for learning C++ Templates? Many thanks.
Recently thanks to rails' popularity, many people start using activerecord as model. however, before
I've been a fan of EasyMock for many years now, and thanks to SO
Many frameworks seek to abstract away from HTML (custom tags, JSFs component system) in
Many applications have grids that display data from a database table one page at
many thanks for this site. many posts have helped me recently. this window question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.