-1

I have a C# script I am trying to use to connect to an Oracle Directory Server Enterprise Edition So far i have had very little success. I am receiving a Unknown error (0x80005000) error message Can somebody tell me what I am doing wrong. I have been researching the web and most online boards say that this error message is because the LDAP in the path needs to be in uppercase letters. As you can see I have done that but still no luck.

Below is my code

 private static readonly string PATH = "LDAP://LDAPDEV.example.com:389/o=example.com";
 private static readonly string USERNAME = uid=SERVICE_USR,ou=ApplicationIDs,o=example.com";
 private static readonly string PASSWORD = "test1234";  


      string DN = "";

        // connect to determine proper distinguishedname
        DirectoryEntry Entry = new DirectoryEntry(Path, USERNAME, PASSWORD, AuthenticationTypes.None);

        try
        {
            // Bind to the native AdsObject to force authentication.
            Object obj = Entry.NativeObject;

            DirectorySearcher Search = new DirectorySearcher(Entry);
            Search.ReferralChasing = ReferralChasingOption.All
        }
        catch (Exception ex)
        {
            throw new Exception("Error looking up distinguishedname. ", ex);
        }
        finally
        {
            anonymousEntry.Close();
        }

        return DN;
    }

string sDomain="LDAPDEV.example.com:389"; 
string sDefaultOU = @"o=example.com"; 
string sServiceUser = @"uid=user,ou=ApplicationIDs,o=example.com"; 
string sServicePassword = "password"; 

try 
 {
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ontextOptions.SimpleBind, sServiceUser,sServicePassword); 

 } 
 catch (Exception ex) 
 {
          ex.Message; 
 }
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • your connection string looks to be a bit off have you read any documentation on how to format a connection string using C# with `LDAP` or `Active Directory here is a stackoverflow posting you can use as an example http://stackoverflow.com/questions/15157746/connection-string-to-connect-to-active-directory-using-ldap do some more research on your end thanks – MethodMan Sep 18 '14 at 15:46
  • Thanks for your response. Did you read my question? It appears the post you pointed me to is referring to AD connections. How about non-AD connections, which is what i am looking into. When i followed the instructions, i am still receiving errors DirectoryEntry entry = new DirectoryEntry("LDAP://address/DC=examples,DC=com", "username", "passwd"); the error i am receiving is "An invalid dn syntax has been specified" Any help you can is much appreciated – user3852972 Sep 19 '14 at 15:33
  • try changing your code and use `PrincipalContext` it's so much easier.. http://stackoverflow.com/questions/11561689/using-c-sharp-to-authenticate-user-against-ldap – MethodMan Sep 19 '14 at 15:52
  • Using PrincipalContext, i am getting an "Object reference not set to an instance of an object." error message below is my code Am i missing something? string sDomain="LDAPDEV.example.com:389"; string sDefaultOU = @"o=example.com"; string sServiceUser = @"uid=user,ou=ApplicationIDs,o=example.com"; string sServicePassword = "pass"; try {PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ontextOptions.SimpleBind, sServiceUser,sServicePassword); } catch (Exception ex) {ex.Message, ex); } – user3852972 Sep 19 '14 at 17:54
  • can't read that code to clearly from comment post it as an amendment to your original question please – MethodMan Sep 19 '14 at 17:57
  • also do you know how to use PrincipalContext..? this is not how you use it.. please look at MSDN site and google PrincipalContext.. – MethodMan Sep 19 '14 at 17:58
  • I have posted the code under section labeled "enter code here" – user3852972 Sep 19 '14 at 18:14
  • once again look up how to user PrincipalContext you do not need all those Parameters .. – MethodMan Sep 19 '14 at 18:45
  • The information i received was from MSDN. Is there a way to see which parameter is causing the issue? I mean is there a tool. I just downloaded softera for the ldap administration and used the connection strings directly in my code. It still does not work. There has to be an easier way to debug this. – user3852972 Sep 19 '14 at 18:45
  • Yes, i will send you the previous tests. All of them produced the same results – user3852972 Sep 19 '14 at 18:46
  • wait..before you head home for miller time..what am i missing by looking at my code? – user3852972 Sep 19 '14 at 18:49
  • I am seeing the same error message no matter what i enter in as a parameter – user3852972 Sep 19 '14 at 18:49
  • once again you are using PrincipalContext Incorrectly in my opinion at first glance looks like this line is incorrect `PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ontextOptions.SimpleBind, sServiceUser,sServicePassword);` also you do not need to have a routine that you can query Active Directory first thing I would do is get the PrincipalContext object to connect and return at least the SamAccount user name.. I will post a simple 1 line example – MethodMan Sep 22 '14 at 14:21

1 Answers1

0

Here is something that you can use to get some information by using PrincipalContext look at this working example and replace the values with your domain name values.

 // if you are doing this via web application if not you can replace
 // the Request/ServerVariables["LOGON_USER"] 
 // with Environment.UserName; this will return your user name like msmith for example so var userName = Environvironment.UserName; 
 var userName = Request.ServerVariables["LOGON_USER"].Split(new string[] {"\\"}, StringSplitOptions.RemoveEmptyEntries);
 var pc = new PrincipalContext(ContextType.Domain,"yourdomain.com",null, null);
 var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName);

this is basically all you need to see the information like email address SamAccountName ect.. use the debugger to inspect all the property values available in the pc variable and userFind variable

MethodMan
  • 18,625
  • 6
  • 34
  • 52