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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:27:26+00:00 2026-05-30T09:27:26+00:00

I need to create a method for my intranet web application that will authenticate

  • 0

I need to create a method for my intranet web application that will authenticate a user using DirectoryServices, either against a default domain, or a user specified one.

On my login form the user will be able to either give there credentials in the form of "username" and "password” or "domain\username" and "password"
The first case can be used when the user is in the same domain as the webserver and is quite straightfoward. The code I use is:

 string domain = "";
 // Code to check if the username is in form of "domain\user" or "user"
 string username = ParseUsername(username, out domain);
 if(domain == "")
    domain = defaultDomain;

 PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, username, password); 
 bool IsAuthenticated = context.ValidateCredentials(username, password)

I pass the username and password to the PrincipalContext constructor in order to bind the call in cases where I try to access another domain.

For the local domain the code works fine. However when I try to check against another domain that is being specified through the username, then I get a “Server could not be contacted” error.

I also tried using different ContextOptions such as ContextOptions.SimpleBind or ContextOptions.Negotiatebut I always seem to be getting the same result.

I need to implement this, since the application is being shipped to various customers, with either single domain or multiple domain environments.
Is there something else I should specify in cases of “remote” domains? The code needs to be flexible since this will be deployed in various environments.

Thanks

EDIT: I must point out, that I prefer to do it using DirectoryServices.AccountManagement and PrincipalContext in order to take advantage of other functionality it provides as well.

Also, I must mention that for my tests, my Dev machine is on a 10.0.0.* network and the second domain I test against is on a 10.0.1.*. I have a route and all, and I can succesfuly connect using an ldap client, so the question is why I cannot connect to the domain via my asp.net application.

  • 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-30T09:27:28+00:00Added an answer on May 30, 2026 at 9:27 am

    I have come up with this solution to the problem.

    In order to support multiple domains, either in trust relationships or even in isolated networks, first of all I added a NameValueCollection in my web.config to list the domains and their domain controllers.

      <domains>
        <add key="domain1" value="10.0.0.1"/>
        <add key="domain2" value="10.0.1.11"/>
      </domains>
    

    (more info on the configuration addition in this so question)

    Then the next step was to read the domain from the User’s credentials in the way I mention in the question. Having gotten the domain I try to lookup the according domain controller from the configuration values, in order to get the proper LDAP connection string. So my method is this:

    private string GetLDAPConnection(string a_Domain, string a_Username, string a_Password)
    {
        // Get the domain controller server for the specified domain
        NameValueCollection domains = (NameValueCollection)ConfigurationManager.GetSection("domains");
        string domainController = domains[a_Domain.ToLower()];
    
        string ldapConn = string.Format("LDAP://{0}/rootDSE", domainController);
    
        DirectoryEntry root = new DirectoryEntry(ldapConn, a_Username, a_Password);
        string serverName = root.Properties["defaultNamingContext"].Value.ToString();
        return string.Format("LDAP://{0}/{1}", domainController, serverName);
    }
    

    Once I get back the proper connection string I make a new call in order to authenticate the user, by addressing the proper LDAP

        ...
        string ldapConn = GetLDAPConnection(domain, username, a_Password);                             
        DirectoryEntry entry = new DirectoryEntry(ldapConn, username, a_Password);        
    
        try
        {
            try
            {
                object obj = entry.NativeObject;
            }
            catch(DirectoryServicesCOMException comExc)
            {
                LogException(comExc);
                return false;
            }
    
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = string.Format("(SAMAccountName={0})", username);
            search.PropertiesToLoad.Add("cn");
            SearchResult result = search.FindOne();
    

    From this point on I can also perform all the other queries I want such as the user’s group membership etc.

    Since the call to the remote domains needs to be bound to a user, I use the "calling" users credentials. This way the user get’s authenticated and the Call is bound to the specific user. Furthermore, I specify a "default" domain, for cases where users provide their credentials without specifying the domain.

    I did not manage to this however using the PrincipalContext as I wanted, but on the bright side, this solution is also applicable for older .NET 2.0 applications as well.

    I am not sure that this is the best solution to the problem, however it seems to work in the tests we have so far performed.

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

Sidebar

Related Questions

Need to create a custom DNS name server using C which will check against
Basically, I need to create a method that will do its darn best to
I need to create an Objective-C method that converts an int into a byte
Everything is working up to here, I just need to create the method that
I need to create an method invoker that any thread (Thread B for example
I create service and I need create class MyErrors (not exception) My web method
i need to create a method, which gets called by a local application with
I need to create an extension method for a GraphicsPath object that determines if
Need to use the create method of the AX 2009 ReturnOrderInService web service in
I'm trying to create default seed records for every user that signs up to

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.