none
AD Authentication Groups RRS feed

  • Question

  • I need a little assistance. I am missing something here. I am using the following article to create a AD log page. For some reason I cant get it to authenticate to a group. I dont want everyone to have access to this. Can anyone assist?

    https://support.microsoft.com/en-us/help/316748/how-to-authenticate-against-the-active-directory-by-using-forms-authen

    • Moved by CoolDadTx Wednesday, December 27, 2017 9:35 PM ASP.NET related
    Wednesday, December 27, 2017 7:51 PM

All replies

  • Please post questions related to ASP.NET and web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, December 27, 2017 9:35 PM
  • This is a C# question
    Thursday, December 28, 2017 12:02 AM
  • Specifically around this 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 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();
         }
       }
    }

    Thursday, December 28, 2017 12:04 AM