User authentication LDAP C# 3.5

Posted on 2009-02-16
Last Modified: 2013-12-19

Could please anyway send a sample code to
1. authenticate a user
2. check if the user is part of a group
3. Show attributes for that user, like name, surname, etc

I have tried different pieces of code I found on Google but nothing seems to work ok.

Question by:i9796674
    LVL 15

    Accepted Solution

    Here is a link for many different AD methods.
    Also the following example may help you out, just set the properties to be the name, surname etc.

    SortedList idList = new SortedList();
                DirectorySearcher search = new DirectorySearcher(domain);
                search.Filter = "(SAMAccountName=" + userName + ")";
                search.SearchScope = SearchScope.Subtree;
                int listIndex = 0;
                SearchResultCollection result = search.FindAll();           
                foreach (SearchResult res in result)
                    foreach (ResultPropertyValueCollection arr in res.Properties.Values)
                        foreach (object obj in arr)
                            idList.Add(listIndex, myString);

    Open in new window


    Author Comment


    Thanks for the code but it does not seem to work.

    I have changed the domain and userName but what should I do with myString? where is it defined?

    I know the page you mention but I have not found an example that works well doing what I wanted to do. I thought to use LDAP on 3.5 it would be something link to the System.DirectoryServices.Protocols namespace... or am I wrong here?

    LVL 15

    Expert Comment

    Whoops!  Thats the problem with a fast copy/paste.  You can ignore the line  idList.Add(listIndex, myString); as that was just me building a specific string and passing it back in the list.  You can publish each object (in foreach (object obj in arr)) to whatever you want and it should give you the list of groups for a specific user.  
    LVL 3

    Assisted Solution


    You have a whole list of samples here

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Read about achieving the basic levels of HRIS security in the workplace.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now