Click here to Skip to main content
15,306,142 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I would like to change a login process for a website - the change is to incorporate Active Directory login - How do I go about doing this ??
and should username and password at login accept uppercase and lowercase ???

Thanks
Posted

Hi,
Here you go:
Validation for a single user to the active directory
C#
public static bool User() 
{
    bool validation;
    try
    {
        LdapConnection lcon = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false));
        NetworkCredential nc = new NetworkCredential(Environment.UserName, "kals123", Environment.UserDomainName);
        lcon.Credential = nc;
        lcon.AuthType = AuthType.Negotiate;
        lcon.Bind(nc); // user has authenticated at this point, as the credentials were used to login to the dc.
        validation = true;
    }
    catch (LdapException)
    {
        validation = false;
    }
    return validation;
}

Lists all the users from current domain
C#
public static void getUser()
{
    DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.UserDomainName);
    string userNames = "";
    string authenticationType="";
    foreach (DirectoryEntry child in directoryEntry.Children)
    {
        if (child.SchemaClassName == "User")
        {
            userNames += child.Name + Environment.NewLine; //Iterates and binds all user using a newline
            authenticationType += child.Username + Environment.NewLine;
        }
    }
    Console.WriteLine("************************Users************************");
    Console.WriteLine(userNames);
    Console.WriteLine("*****************Authentication Type*****************");
    //Console.WriteLine(authenticationType);
}

Getting user names with groups
C#
public static void fnGetListOfUsers() {
    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

    // find the group in question
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "USERS");

    // if found....
    if (group != null)
    {
        // iterate over members
        foreach (Principal p in group.GetMembers())
        {
            Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
            // do whatever you need to do to those members
        }
    }
}

Getting a particular user details from user's active directory
C#
public static void fnImp() {
    using (var context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
    {
        using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
        {
            foreach (var result in searcher.FindAll())
            {
                DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                if ((string)de.Properties["givenName"].Value == Environment.UserName)
                {
                    //Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
                    //Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
                    //Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
                    //Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
                    Console.WriteLine();
                    PropertyCollection pc = de.Properties;
                    foreach (PropertyValueCollection col in pc)
                    {
                        Console.WriteLine(col.PropertyName + " : " + col.Value);
                        Console.WriteLine();
                    }
                }
            }
        }
    }
    Console.ReadLine();
}



What you need is all here.
--Amit
   
Comments
AmitGajjar 28-Jul-12 1:12am
   
nice 5+
_Amy 28-Jul-12 1:15am
   
Thank you Amit.
We would need more specific information to help you with your problem. And yes the login SHOULD accept uppercase and lowercase for the password, if your using a hash algorithm then I don't see why your asking about that.

I suggest you take a look here [^] and here [^]

As a general tip, people like to help you with your coding problems here, not give you a step by step how to. They will ask you to Google your solution, if it is simple they will ridicule you if for ask a stupid question. I learned from experience, Google is your best friend for simple questions.
   
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900