[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

access active directory for login destop form using C#

Posted on 2006-07-24
4
Medium Priority
?
706 Views
Last Modified: 2008-02-07
i want to make a login desktop form using C#2005 to make the user enter the username and password of the windows xp in this application and this application must check if this username and password valid or not , i give a code from msdn that make me do this but i have aproblem  the code is

using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;


public class LdapAuthentication
{
    private string _path;
    private string _filterAttribute;

    public LdapAuthentication(string path)
    {
        _path = path;
    }

    public bool IsAuthenticated(string domain, string username, string pwd)
    {
        string domainAndUsername = domain + @"\" + username;
        DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

        try
        {
            //Bind to the native AdsObject to force authentication.
            object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);

            search.Filter = "(SAMAccountName=" + username + ")";
            search.PropertiesToLoad.Add("cn");
            SearchResult result = search.FindOne();

            if (null == result)
            {
                return false;
            }

            //Update the new path to the user in the directory.
            _path = result.Path;
            _filterAttribute = (string)result.Properties["cn"][0];
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }

        return true;
    }

    public string GetGroups()
    {
        DirectorySearcher search = new DirectorySearcher(_path);
        search.Filter = "(cn=" + _filterAttribute + ")";
        search.PropertiesToLoad.Add("memberOf");
        StringBuilder groupNames = new StringBuilder();

        try
        {
            SearchResult result = search.FindOne();
            int propertyCount = result.Properties["memberOf"].Count;
            string dn;
            int equalsIndex, commaIndex;

            for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
            {
                dn = (string)result.Properties["memberOf"][propertyCounter];
                equalsIndex = dn.IndexOf("=", 1);
                commaIndex = dn.IndexOf(",", 1);
                if (-1 == equalsIndex)
                {
                    return null;
                }
                groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                groupNames.Append("|");
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error obtaining group names. " + ex.Message);
        }
        return groupNames.ToString();
    }
}



and when i am use it by

            LdapAuthentication ld = new LdapAuthentication("LDAP://rootDSE");
         ld.IsAuthenticated("isasrv", "aaa", "bbb");
which is the first argument is the server name , the seconde is the user name the third is the password
i take exception
The providers deose not support searching and canot search LDAP\\:rootDSE
,pleas help me .




and i am not sure about the active directory path is it true .

note this applcation in the msdn web site to a web application not to desktop application , i want to desktop abblication

thanx

0
Comment
Question by:moodyahmad
2 Comments
 
LVL 10

Accepted Solution

by:
prakash_prk earned 1000 total points
ID: 17167494
could u check this plz.

LdapAuthentication ld = new LdapAuthentication("LDAP://isasrv");
ld.IsAuthenticated("isasrv", "aaa", "bbb");

also try the other overload fucntion of DirectoryEntry class as shown in the article.
http://www.codeproject.com/csharp/arbauthentication.asp

regards
prakash
0
 

Author Comment

by:moodyahmad
ID: 17168026
i am using visual studio 2005
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2

830 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