Solved

access active directory for login destop form using C#

Posted on 2006-07-24
4
694 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
4 Comments
 
LVL 10

Accepted Solution

by:
prakash_prk earned 500 total points
Comment Utility
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
Comment Utility
i am using visual studio 2005
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

16 Experts available now in Live!

Get 1:1 Help Now