I am using the following code to get user email adrees from active directory.
string _path = "LDAP://domain/DC=dc,DC=com"; string domainAndUsername = domain + @"\" + username; DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); try { // Bind to the native AdsObject to force authentication. Object obj = entry.NativeObject; DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + username + ")"; search.PropertiesToLoad.Add("mail"); SearchResult result = search.FindOne(); if (result != null) { string mail=result.Properties["mail"][0].ToString(); }
But, I am getting System.ArgumentOutOfRangeException: ( Index was out of range. Must be non-negative and less than the size of the collection)
For cn,everything works fine.What is the problem with .Properties["mail"]. Please help me to solve the issue
//code for cn search.PropertiesToLoad.Add("cn");string cn=result.Properties["cn"][0].ToString();
If the account has not have the mail attribute populated you will get an error if you try and access it. You can use an if statement with result.properties.contains("mail") to check before trying to access the value
Important : the account that performs the search must be a member of the Administrators group to retrieve the ntSecurityDescriptor property. If this is not the case, the property value that is returned for ntSecurityDescriptor will be null.
Please 'Mark as Answer' if my post helped you
--------------------------------------------------
Muhammad Amin
محمد امين
harinarayana...
Member
63 Points
130 Posts
Out of range exception while trying to get user email address from Active Directory
Mar 21, 2012 10:57 AM|LINK
Hi,
I am using the following code to get user email adrees from active directory.
string _path = "LDAP://domain/DC=dc,DC=com"; string domainAndUsername = domain + @"\" + username; DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); try { // Bind to the native AdsObject to force authentication. Object obj = entry.NativeObject; DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + username + ")"; search.PropertiesToLoad.Add("mail"); SearchResult result = search.FindOne(); if (result != null) { string mail=result.Properties["mail"][0].ToString(); }But, I am getting System.ArgumentOutOfRangeException: ( Index was out of range. Must be non-negative and less than the size of the collection)
For cn,everything works fine.What is the problem with .Properties["mail"]. Please help me to solve the issue
mameenkhn
Contributor
2026 Points
391 Posts
Re: Out of range exception while trying to get user email address from Active Directory
Mar 21, 2012 11:58 AM|LINK
try
--------------------------------------------------
Muhammad Amin
محمد امين
gww
Contributor
2143 Points
458 Posts
Re: Out of range exception while trying to get user email address from Active Directory
Mar 21, 2012 01:04 PM|LINK
harinarayana...
Member
63 Points
130 Posts
Re: Out of range exception while trying to get user email address from Active Directory
Mar 26, 2012 01:19 PM|LINK
Hi,
is giving the name,.not mail id
harinarayana...
Member
63 Points
130 Posts
Re: Out of range exception while trying to get user email address from Active Directory
Mar 26, 2012 01:25 PM|LINK
if(search.PropertiesToLoad.Contains("mail") is giving true. Means the properties contains mail. Please help
mameenkhn
Contributor
2026 Points
391 Posts
Re: Out of range exception while trying to get user email address from Active Directory
Mar 26, 2012 04:35 PM|LINK
http://forums.asp.net/p/907421/1007517.aspx
check the above link it might help
also check these two properties
proxyAddresses
targetAddress
Important : the account that performs the search must be a member of the Administrators group to retrieve the ntSecurityDescriptor property. If this is not the case, the property value that is returned for ntSecurityDescriptor will be null.
--------------------------------------------------
Muhammad Amin
محمد امين
gww
Contributor
2143 Points
458 Posts
Re: Out of range exception while trying to get user email address from Active Directory
Mar 27, 2012 11:48 AM|LINK
You need to change that line to the following to get the email
mameenkhn
Contributor
2026 Points
391 Posts
Re: Out of range exception while trying to get user email address from Active Directory
Mar 28, 2012 07:55 AM|LINK
hi harinarayananvp
I want to know which code worked for you
--------------------------------------------------
Muhammad Amin
محمد امين