How to authenticate a windows user in Active Directory using C#

Yanıt How to authenticate a windows user in Active Directory using C#

  • 12 Mart 2012 Pazartesi 22:14
     
      Kod İçerir

    Hi,

    I've a question on validating Windows logged user in Active Directory using c#.  

    PrincipalContext adContext = new PrincipalContext(ContextType.Domain);
    
                    //WindowsPrincipal P = Thread.CurrentPrincipal as WindowsPrincipal;
                    //string sss = P.Identity.Name;
                    
    
                    using (adContext)
                    {
                        return adContext.ValidateCredentials(Environment.UserName, "xxx");
                    }
    

    I used above code snippet, but its validating only when i provide password.  I want to validate while user double-clicks my application (.exe).  While loading the application, i need to authenticate his credentials(Windows login details) with Active Directory whether he is valid user from specific USer Group.

    Here the case i wont get any input from user when he double-clicks my application, i've to use Windows credentials to validate.  Can any body helps me to resolve this Active Directory validation without providing password.



    S. Ramkumar

Tüm Yanıtlar

  • 13 Mart 2012 Salı 00:03
     
     Yanıt

    Not sure I follow.  If the user was able to log in the PC, the user is already authenticated.  Just get the current principal and have piece of mind that the user has been authenticated already.  If you also want group membership, well that's different.  That would not be authentication, that would be a mechanism of authorization.

    Obtain the WindowsPrincipal object using System.Threading.Thread.CurrentPrincipal, then call the IsInRole() method.


    Jose R. MCP

    • Yanıt Olarak İşaretleyen Ramkumar_TPS 13 Mart 2012 Salı 15:52
    •  
  • 13 Mart 2012 Salı 09:22
     
     Yanıt Kod İçerir

    Hi, 

    As webJose mentioned, If user is logged-in to the system means he is already authenticated. Are you looking for is user belongs to specific group?

    If this is the case, get the groups list that he belongs to and validate against the group who can, for this you can use

               var appDomain = Thread.GetDomain();
                appDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                var windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
                var groups = ((WindowsIdentity)windowsPrincipal.Identity).Groups;

     Hope this helps you...

    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    • Yanıt Olarak İşaretleyen Ramkumar_TPS 13 Mart 2012 Salı 15:52
    •  
  • 13 Mart 2012 Salı 15:53
     
     
    Thanks WebJose and Kris444, i'll follow your ideas.

    S. Ramkumar