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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:08:37+00:00 2026-05-27T19:08:37+00:00

I have a class that have three methods that have functionalities on Active Directory.

  • 0

I have a class that have three methods that have functionalities on Active Directory. Here is the class:

[Export(typeof(IAuthentication))]
public class Authentication : IAuthentication
{
    public bool Authenticate(string domain, string username, string password) 
    {
        try
        {
            using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domain, string.Empty))
            {
                return principalContext.ValidateCredentials(
                    username,
                    password,
                    ContextOptions.SimpleBind);
            }
        }
        catch (Exception ex)
        {   
            throw ex;
        }
    }

    public UserPrincipal GetUserDetails(string domain, string username) 
    {
        try
        {
            using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domain))
            {
                return UserPrincipal.FindByIdentity(principalContext, username);
            }
        }
        catch (Exception ex)
        {   
            throw ex;
        }
    }

    public PrincipalSearchResult<Principal> SearchUsers(string domain, string firstName, string lastName, string userName)
    {
        try
        {
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain))
            {
                UserPrincipal user = new UserPrincipal(ctx);
                user.Enabled = true;
                user.Name = firstName + "* " + lastName + "*";
                user.SamAccountName = userName + "*";

                PrincipalSearcher principalSearcher = new PrincipalSearcher();
                principalSearcher.QueryFilter = user;

                return principalSearcher.FindAll();
            }
        }
        catch (Exception ex)
        {   
            throw ex;
        }
    }
}

As you see in the class attribute, I’m using this class library as a MEF plugin. In my asp.net mvc 3 application, I call the method like this:

PrincipalSearchResult<Principal> results = _authentication.SearchUsers(
                                                        ConfigurationManager.AppSettings["DomainName"], 
                                                        model.UserSearchCriteria.FirstName, 
                                                        model.UserSearchCriteria.LastName, 
                                                        model.UserSearchCriteria.Username);

But after I intend to use the return value of the method, I’m getting Cannot access a disposed object, Object name: ‘PrincipalContext’ exception. I know I’m disposing the PrincipalContext object but if I do not, the connection to the Active Directory will stay open. I think the design of my class is not correct. How can I make it work in a cool way?

  • 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-27T19:08:38+00:00Added an answer on May 27, 2026 at 7:08 pm

    I would imagine that when you eventually enumerate over the instance of PrincipalSearchResult<T>, this deferred operation is trying to access the context, which has since been closed. In this scenario, it is likely better for you to enumerate over the results straight away and return them as domain-specific models. I.e, do the work at the time, and not defer it through returning PrincipalSearchResult<T>.

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

Sidebar

Related Questions

I have three files: one called sql.php witch has a class db that I
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have some code that looks like: class Parent { private Intermediate intermediateContainer; public
Please take a look at my simple three-methods Service class that streams audio and
I have a class that I have to call one or two methods a
I have class which has a method that needs to return three DataTables. I
I have three methods that all access the same files in on folder. I
I have a class named NodeUtil and that class have a three services injected
I have a binary tree class that is created with a root node and

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.