Is there a way to get users full path in active directory in ASP.Net Core?

623 views Asked by At

I need to validate a specific user's path in active diretory in order to let him to autenticate to my website. Is there a method that I can user to get the full path. I've lood into PrincipalSearch but I don't think that works for a specific user, it searches the full AD. Here is what I have so far:

  PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, ip, container, user, pass);
  UserPrincipal userAD = UserPrincipal.FindByIdentity(principalContext, username);
                
                
                if (userAD != null)
                {

                    if (principalContext.ValidateCredentials(username, password))
                    {

                        
                        return true;

                    }
                    else
                    {
                        return false;
                    }

I wanted to get the full path in the Active Directory for the user that is used in the userAD variable. How should I do this? Any advice?

1

There are 1 answers

1
Sam On BEST ANSWER

I used the method DistinguishedName in order to get the location of the user. The code is now something like this:

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, ip, container, user, pass);
UserPrincipal userAD = UserPrincipal.FindByIdentity(principalContext, username);
 var path = userAD.DistinguishedName;             
            
            if (userAD != null)
            {
              if(path.Contains("blabla")){

                if (principalContext.ValidateCredentials(username, password))
                {

                    
                    return true;

                }
                else
                {
                    return false;
                }
             }

The DistinguishedName returns the full path of the user specified in userAD. Hope this is useful!