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

  • Home
  • SEARCH
  • 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 467721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:36:37+00:00 2026-05-12T23:36:37+00:00

I recently refactored some code in an Active Directory role provider to remove support

  • 0

I recently refactored some code in an Active Directory role provider to remove support for multiple domains. In the process my integration tests broke in ways that I didn’t expect. The tests do not reliably succeed unless I put significant delays between the test set up code and the code that invoked the method being tested. If I run the test using the debugger it always succeeds and I can’t see any problems with the code. If I run the test using the automated tools one or more tests fail and fail in ways that are unexpected.

How can I reliabily test role provider code that uses the System.Directory.AccountManagement namespace classes and methods?

Note: In keeping with the SO paradigm, I’m providing the solution that I found as a separate answer. I’m open to other solutions, however, if you feel that your solution works better than mine. This question is being contributed because I couldn’t find any existing questions on SO that addressed my problem.

Some related questions are:

  • How to unit-test a NextPasswordChangeDate function against the Active Directory
  • How to setup a Active Directory environment test?
  • 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-12T23:36:37+00:00Added an answer on May 12, 2026 at 11:36 pm

    I discovered that the problem was that the PrincipalSearchers that I was using in the role provider did not always contact the same domain controller as the code used in set up did. This would result in errors due to propagation delays between domain controllers. To solve this problem I used constructor injection to provide the PrincipalContext used in set up to the role provider. This allows the role provider to always use the same context as the test code. In addition I replaced the SearchRoot on the PrincipalSearcher with a search root based on the PrincipalContext provided via constructor injection. Relevant code below. Note that the role provider implements IDisposable in order to dispose of the domain context if one isn’t supplied externally.

    private bool DisposeContext { get; set; }
    private PrincipalContext DomainContext { get; set; }
    
    public PrintAccountingRoleProvider() : this( null ) { }
    
    public PrintAccountingRoleProvider( PrincipalContext domainContext )
    {
        this.DisposeContext = domainContext == null;
        this.DomainContext = domainContext ?? new PrincipalContext( ContextType.Domain );
    }
    

    …

    private UserPrincipal FindUser( string userName )
    {
        using (PrincipalSearcher userSearcher = new PrincipalSearcher())
        {
            UserPrincipal userFilter = new UserPrincipal( this.DomainContext );
            userFilter.SamAccountName = userName;
            userSearcher.QueryFilter = userFilter;
    
            // Replace the searcher with one directly associated with the context to ensure that any changes
            // made elsewhere will be reflected if we call the search immediately following the change.  This
            // is critical in the integration tests.
            var searcher = userSearcher.GetUnderlyingSearcher() as DirectorySearcher;
            searcher.SearchRoot = new DirectoryEntry( @"LDAP://" + this.DomainContext.ConnectedServer + @"/dc=iowa,dc=uiowa,dc=edu" );
    
            return userSearcher.FindOne() as UserPrincipal;
        }
    }
    

    …

    private void Dispose( bool disposing )
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                if (this.DisposeContext && this.DomainContext != null)
                {
                    this.DomainContext.Dispose();
                    this.DomainContext = null;
                }
            }
            this.disposed = true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Its sent as a wide-character string to SQL server --… May 13, 2026 at 1:17 am
  • Editorial Team
    Editorial Team added an answer On your server, create a folder called myapp. Chdir to… May 13, 2026 at 1:17 am
  • Editorial Team
    Editorial Team added an answer You're looking for Bezier Curves, or some other similar parametric… May 13, 2026 at 1:17 am

Related Questions

I recently refactored some of my code to stuff rows into a db using
I recently wrote a quick-and-dirty proof-of-concept proxy server in C# as part of an
I'm currently modifying a class that has 9 different constructors. Now overall I believe
I've been tinkering with small functions on my own time, trying to find ways
I recently started a project and needed to do some integration with LDAP via

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.