0

I am trying to connect to an LDAP server in C# using PrincipalContext. I've validated the network parameters and authentication settings are all correct using Apache Directory Studio. This is the code I'm using:

string sDomain = @"vds.myCompany.net:1234";
string sDefaultOU = @"ou=apdev,ou=ads,o=vds";
string sUser = @"myUsername";
string sPassword = @"myPassword";

ContextOptions options = ContextOptions.SimpleBind | ContextOptions.SecureSocketLayer;
PrincipalContext context = new PrincipalContext(ContextType.Domain, sDomain, null, options, sUser, sPassword);

I must connect using SSL and my credentials are SSO. I've tried with and without specifying the sDefaultOU as the container parameter. The error it throws at the last line is:

System.NullReferenceException: Object reference not set to an instance of an object. at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties) at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)

elisa
  • 23
  • 1
  • 2
  • 6
  • Do you have `SSL` installed? Does your user has enough rights? Try simply binding without specifying `username` and `password`. `PrincipalContext context = new PrincipalContext(ContextType.Domain, sDomain)`. – smr5 Aug 19 '15 at 03:58
  • How would I know if I have SSL installed? I can connect successfully via Apache Directory Studio and Pentaho. – elisa Aug 19 '15 at 12:49
  • Here's what I get when I try binding without user/pw: System.DirectoryServices.Protocols.DirectoryOperationException: The server is unavailable. at System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32 messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, Boolean exceptionOnTimeOut) at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout) at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request) – elisa Aug 19 '15 at 12:51
  • Ask you sys admin if `SSL` installed. But to check it yourself, set your `ContextOptions` just to `ContextOptions.SecureSocketLayer`. If you don't have it's going to throw a message. Unless you changed it, the default port is `636`. You're connecting to `1234` Is that intentionally? – smr5 Aug 19 '15 at 17:00
  • Setting ContextOptions just to ContextOptions.SecureSocketLayer throw this error: System.ArgumentException: The ContextOptions passed are invalid for this store type. Either Negotiate or SimpleBind must be specified and they cannot be combined. at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password) at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options) – elisa Aug 19 '15 at 21:00
  • I'm not connecting to 636 or 1234, I'm just not allowed to post the actual port number. – elisa Aug 19 '15 at 21:01
  • That's what I was expecting to see. This message `The ContextOptions passed are invalid for this store type` tells you you don't have `SSL` installed. I'm guessing you're using `389` which is not secure. To connect without SSL you can use `PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomian.com", container, ContextOptions.SimpleBind);` – smr5 Aug 19 '15 at 21:53

0 Answers0