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 8654221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:43:59+00:00 2026-06-12T14:43:59+00:00

I am creating accounts and setting properties on them using System.DirectoryServices.AccountManagement in .NET 4.5.

  • 0

I am creating accounts and setting properties on them using System.DirectoryServices.AccountManagement in .NET 4.5. One of the requirements is that the group membership (including Primary Group) be copied from a template account. The code includes the following:

foreach (var group in userPrincipal.GetGroups()) {
    var groupPrincipal = (GroupPrincipal) @group;

    if (groupPrincipal.Sid != templatePrimaryGroup.Sid) {
        groupPrincipal.Members.Remove(userPrincipal);
        groupPrincipal.Save();
    }
}

This works about 90% of the time. The rest of the time, it fails with:

System.DirectoryServices.DirectoryServicesCOMException was
unhandled HResult=-2147016656 Message=There is no such object on
the server.

Source=System.DirectoryServices ErrorCode=-2147016656
ExtendedError=8333 ExtendedErrorMessage=0000208D: NameErr:
DSID-03100213, problem 2001 (NO_OBJECT), data 0, best match of:
'OU=Whatever,DC=domain,DC=local`

on the GetGroups call. My guess is that there is a race condition of some sort with the user not being fully created before I next go to access it. I know from diagnostic logging that I am going against the same domain controller each time (it’s using the same PrincipalContext so that matches my expectation) so it’s not a replication issue.

Is my guess accurate? Is there a good way to handle this? I could just throw in a Sleep but that seems like a cop-out at best and fragile at worst. So what is the right thing to do?

  • 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-06-12T14:44:00+00:00Added an answer on June 12, 2026 at 2:44 pm

    Try something like:

            int maxWait = 120;
            int cnt = 0;
            bool usable = false;
    
            while (usable == false && cnt < maxWait)
            {
                try
                {
                    foreach (var group in userPrincipal.GetGroups())
                    {
                        var groupPrincipal = (GroupPrincipal)@group;
    
                        if (groupPrincipal.Sid != templatePrimaryGroup.Sid)
                        {
                            groupPrincipal.Members.Remove(userPrincipal);
                            groupPrincipal.Save();
                        }
                    }
                    usable = true;
                    break;
                }
                catch
                {
                    System.Threading.Thread.Sleep(500);
                }
            }
            if (usable)
                //All okay
                ;
            else
                //Do something
                ;
    

    This way you can try for “a while”. If it works good, if not do something like log an error, so you can run a fix-it script later.

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

Sidebar

Related Questions

I am creating a web-page/website that integrates all my accounts into one spectrum, as
I have read that the easiest way for setting up a connection and creating
I'm using asp .net mvc 4. I have a database that contains a table
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I'm using a proprietary platform that reported memory usage in realtime on screen. I
Possible Duplicate: Android Emulator - Trouble creating user accounts I'm trying to register a
I'm creating a application pool in iis 6.0 using code, but haven't found a
I am developing an ASP.Net application that will need to verify that the user
Does anybody know how i can go about creating email accounts and maybe read
I am wondering what the best practices are for creating non-loggable accounts. On StackOverflow,

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.