locked
Accessing DirectoryEntry NativeObject property RRS feed

  • Question

  • User-1556315445 posted

    When I use AuthenticationType.Secure  I get error when trying to access NativeObject property (COMException). However, the same code without  AuthenticationType set works fine. Why does it happen?

    Sunday, February 5, 2006 2:37 PM

All replies

  • User-1556315445 posted

    Well, ... the error was not exactly where I pointed to in the previus message.

    Here is the code which works fine (taken from an articel on MSDN):

    private static bool IsAuthenticated(string domain, string username, string pwd){

    string domainAndUsername = domain + @"\" + username;

    string path = LDAP://ServerName/DC=xxx,DC=xxx,DC=xxx;

    DirectoryEntry entry = new DirectoryEntry(path, domainAndUsername,pwd,AuthenticationTypes.Secure | AuthenticationTypes.Sealing);

    try{

          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;}

    }

       catch (Exception ex)

    {throw new Exception("Error authenticating user. " + ex.Message);}

       return true;

    }

     

    Here is the one which fails. It is a little more complex, but it "should be faster":

    private static void AuthenticateUser(string userName,string password){

    try{

    using (DirectoryEntry entry = FindUser(userName,password ))

    {

    if( entry != null )

    {

    string adsFullPath = entry.Path;

    string userADSPath = entry.Properties["distinguishedName"][0].ToString();

    DirectoryEntry user = new DirectoryEntry (adsFullPath, userADSPath, password, AuthenticationTypes.Secure | AuthenticationTypes.Sealing);

    try{

    object native = user.NativeObject;}

    catch (Exception e){

    Console.WriteLine(e.Message);

    }

    finally{

    if (user != null)

    user.Dispose();}

    }

    }

    }

    catch( Exception ex )

    {

    Console.WriteLine(ex.Message);

    }

    }

    private static DirectoryEntry FindUser( string userName, string password )

    {

    string adsPath = LDAP://ServerName/DC=xxx,DC=xxx,DC=xxx;

    DirectoryEntry entry = null;

    DirectoryEntry foundUser = null;

    try

    {

    entry = new DirectoryEntry (adsPath, userName, password, AuthenticationTypes.Secure | AuthenticationTypes.Sealing);

    string filter = string.Format( "(&(objectCategory=person)(sAMAccountName={0}))", userName );

    using (DirectorySearcher search = new DirectorySearcher(entry, filter, new string[]{"cn"}))

    {

    SearchResult searchResult = null;

    using (SearchResultCollection src = search.FindAll())

    {

    if (src.Count > 0)

    {

    searchResult = src[0];

    }

    }

    if ( searchResult != null )

    {

    foundUser = searchResult.GetDirectoryEntry();

    }

    }

    }

    finally{

    if (entry != null)

    entry.Dispose();}

    return foundUser;

    }

     

    The point is that SearchResultCollection in the "FindUser" method returns nothing and as a result  NativeObject property in the "AuthenticateUser" throws error.

    So really my question is why specifing more accurate path(s) results in failed search?

    Thanks for your help.

     

    Monday, February 6, 2006 1:50 PM