Active Directory Authentication

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • piggy

    Active Directory Authentication

    I have a login name with user name and password. based on provided username
    and password, those infor will be looked up in the active directory for a
    specific group to authenticate users. Could you please help me with that? i
    have found a code to do that but fail to connect to the AD with provided
    username and password. In order to connect to the AD, what access level is
    needed here. Your help will be appreciate. Thanks,
  • Willy Denoyette [MVP]

    #2
    Re: Active Directory Authentication


    "piggy" <piggy@discussi ons.microsoft.c omwrote in message
    news:1306D2B8-7DCE-4FC1-B192-9CFF1C55160B@mi crosoft.com...
    |I have a login name with user name and password. based on provided
    username
    | and password, those infor will be looked up in the active directory for a
    | specific group to authenticate users. Could you please help me with that?
    i
    | have found a code to do that but fail to connect to the AD with provided
    | username and password. In order to connect to the AD, what access level
    is
    | needed here. Your help will be appreciate. Thanks,

    There is no access level needed, you have to specify correct credentials and
    UthenticationTy pes when binding.
    Please post your code.

    Willy.


    Comment

    • piggy

      #3
      Re: Active Directory Authentication

      Here is the path. Is it corrrect?
      "LDAP://companyname.org/OU=Office,DC=PD C,DC=companynam e,DC=org"

      Thanks,

      public bool IsAuthenticated User(string domain, string userName, string
      password)
      {

      //string domainAndUserna me = domain + @"\" + userName;
      string domainAndUserna me = userName + "@" + domain;
      // Connect to SHP AD from DirectoryEntry object
      DirectoryEntry entry = new DirectoryEntry( _path,
      domainAndUserna me, password);

      try
      {
      // Bind to the native AdsObject to force authentication.
      Object obj = entry.NativeObj ect;
      // Get user from directory based on their login name
      DirectorySearch er search = new DirectorySearch er(entry);
      search.Filter = "(SAMAccountNam e=" + userName + ")";
      search.Properti esToLoad.Add("c n");
      // If more than one entry is found, only return the first
      entry. If no entry
      // is found, return null
      SearchResult result = search.FindOne( );
      if (null == result)
      {
      return false;
      }
      // Update the new path to the user in the directory
      _path = result.Path;
      _filterAttribut e = (String)result. Properties["cn"][0];
      }
      catch (Exception ex)
      {
      throw new Exception("Erro r authenticating user. " +
      ex.Message);
      }
      return true;
      }

      // Retrieve the list of group that a user is a member of the AD
      public string GetUserGroup()
      {
      DirectorySearch er search = new DirectorySearch er(_path);
      search.Filter = "(cn=" + _filterAttribut e + ")";
      search.Properti esToLoad.Add("m emberOf");
      StringBuilder groupNames = new StringBuilder() ;
      try
      {
      SearchResult result = search.FindOne( );
      int propertyCount = result.Properti es["memberOf"].Count;
      String dn;
      int equalsIndex, commaIndex;

      groupNames.Appe nd("|");
      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.Appe nd(dn.Substring ((equalsIndex + 1),
      (commaIndex - equalsIndex) - 1));
      groupNames.Appe nd("|");
      }
      }
      catch (Exception ex)
      {
      throw new Exception("Erro r obtaining group names. " +
      ex.Message);
      }
      return groupNames.ToSt ring();

      }

      "Willy Denoyette [MVP]" wrote:
      >
      "piggy" <piggy@discussi ons.microsoft.c omwrote in message
      news:1306D2B8-7DCE-4FC1-B192-9CFF1C55160B@mi crosoft.com...
      |I have a login name with user name and password. based on provided
      username
      | and password, those infor will be looked up in the active directory for a
      | specific group to authenticate users. Could you please help me with that?
      i
      | have found a code to do that but fail to connect to the AD with provided
      | username and password. In order to connect to the AD, what access level
      is
      | needed here. Your help will be appreciate. Thanks,
      >
      There is no access level needed, you have to specify correct credentials and
      UthenticationTy pes when binding.
      Please post your code.
      >
      Willy.
      >
      >
      >

      Comment

      Working...