Active Directory - Null Reference Exception
-
Hi Im trying to retrieve user properties from active directory. It successfully gets the first required property, but after that all i get is 'NullReferenceException - Object reference not set to an instance of an object' Why is this? Here is the code:
DirectoryEntry standardUser = new DirectoryEntry("LDAP://CN=" + txtUser.Text.ToString() + ",OU=Users - Standard Users,DC=mydomain,DC=com,DC=uk", "username", "password"); string sUSN = standardUser.Properties["userPrincipalName"].Value.ToString(); string nastsprofile = (standardUser.Properties["TerminalServicesProfilePath"].Value.ToString());
The bit which gets the userPrincipalName works fine. After that it all goes wrong. -
Hi Im trying to retrieve user properties from active directory. It successfully gets the first required property, but after that all i get is 'NullReferenceException - Object reference not set to an instance of an object' Why is this? Here is the code:
DirectoryEntry standardUser = new DirectoryEntry("LDAP://CN=" + txtUser.Text.ToString() + ",OU=Users - Standard Users,DC=mydomain,DC=com,DC=uk", "username", "password"); string sUSN = standardUser.Properties["userPrincipalName"].Value.ToString(); string nastsprofile = (standardUser.Properties["TerminalServicesProfilePath"].Value.ToString());
The bit which gets the userPrincipalName works fine. After that it all goes wrong.Well, tha fact that you can get the userPrincipalName means that standardUser is fine, so the problem must be that
standardUser.Properties["TerminalServicesProfilePath"].Value
is null. I think you might need a lower case 'T' or something. I found this similar problem on t'internet HERE[^], where the apparent solution was to use lower case.My current favourite word is: Bacon!
-SK Genius
-
Well, tha fact that you can get the userPrincipalName means that standardUser is fine, so the problem must be that
standardUser.Properties["TerminalServicesProfilePath"].Value
is null. I think you might need a lower case 'T' or something. I found this similar problem on t'internet HERE[^], where the apparent solution was to use lower case.My current favourite word is: Bacon!
-SK Genius
-
Ive followed the link. And changed it to all lowercase. And replaced .value with .invokeget. Still no avail
OK, i think i have it. I'm quite sure you do need to use InvokeGet. InvokeGet will return an object, so you'll need to cast it as a string.
string nastsprofile = standardUser.InvokeGet("TerminalServicesProfileName") as string;
orstring nastsprofile = standardUser.InvokeGet("terminalServicesProfileName") as string;
orstring nastsprofile = standardUser.InvokeGet("terminalservicesprofilename") as string;
I'm pretty sure one of those should work.My current favourite word is: Bacon!
-SK Genius