Results 1 to 3 of 3

Thread: Active Directory User Validation + ASP.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    24

    Active Directory User Validation + ASP.net

    Hi, Experts I have developed a web application which is running smoothly and I am validating user role from a table. Now I want to use Active Directory concept to validate users and access control .

    Now how to do that? If any one give me running code or helpful link .. will appreciate

    I have developed web application using ASP.net + Vb.net + sql Server

    Thanks

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Active Directory User Validation + ASP.net

    You can set the Directory Security of your web application to use Windows Authentication.

    I wrote this class to get a little information on the user, it's very very basic but it should be able to get you started on other user details using Active Directory:

    PHP Code:
    using System;
    using System.Text;
    using System.Collections;
    using System.DirectoryServices;
            
    namespace 
    FormsAuth
    {    
        public class 
    LdapAuthentication
        
    {
            private 
    string _path;
            private 
    string _filterAttribute;
            
            public 
    LdapAuthentication(string path)
            {
                
    _path path;
            }
                    
            public 
    bool IsAuthenticated(string domainstring usernamestring 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();
            }
        }

    Yes, C#, but I'm sure you can convert.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    24

    Re: Active Directory User Validation + ASP.net

    Hi,

    Thanks for the help.. I will try to convert in Vb.net ..

    Appreciate your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width