I'm currently accessing active directory using C#. My task requires me to find a Exchange mailbox user entry in remote active directory. My code works perfectly sometimes and sometimes it throws an error like : "A referral was returned from the server" or
"An operation Error occurred", etc.
Please view my code and provide any inputs to solve my problem.
Code is given below:-
public bool OpenConnection()
{
try
{
de = new DirectoryEntry("LDAP://" + server + ":" + _port + "/" + _partitionDir, _login, _password, AuthenticationTypes.None);
if (de != null)
{
using (var DE = de)
{
try
{
DE.RefreshCache(); // This will force credentials validation
Console.WriteLine("");
Console.WriteLine("Exchange 2007 LDAP Validation successful");
Console.WriteLine("");
}
catch (COMException ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
else
{
return false;
throw new NullReferenceException();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool MailboxUserExists(string username)
{
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.ReferralChasing = ReferralChasingOption.All;
deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))";
SearchResultCollection results = deSearch.FindAll(); //ERROR OCCURS
try
{
if (results.Count > 0)
{
Console.WriteLine("User Found: " + results.Count);
return true;
}
else
{
Console.WriteLine("User Not Found");
return false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
I've tried to take console output of my LDAP string I've passed and it is perfectly fine. Also using same LDAP connection string, I'm able to update AD fields for a particular user.
I don't know why the code fails occassionaly.
If there is no problem in code and if no solution is available, please provide any suggestions of handling these kind of active directory errors.
Thanks in advance.
Thanks for a quick response and the given links. I've refer to all the links and found no difference between the given code on the links and my own code. I've mentioned that my code works perfectly for most of the time. It's just sometime it throws these
exceptions:- "a referral was returned from the server" or "An operation error occurred".
Can i manage these exceptions by making any changes to my existing code?? Please provide suggestions.
ASHU_TECHIT
0 Points
2 Posts
Active Directory Error
Jun 30, 2012 07:42 AM|LINK
Hi Everyone,
I'm currently accessing active directory using C#. My task requires me to find a Exchange mailbox user entry in remote active directory. My code works perfectly sometimes and sometimes it throws an error like : "A referral was returned from the server" or "An operation Error occurred", etc.
Please view my code and provide any inputs to solve my problem.
Code is given below:-
public bool OpenConnection() { try { de = new DirectoryEntry("LDAP://" + server + ":" + _port + "/" + _partitionDir, _login, _password, AuthenticationTypes.None); if (de != null) { using (var DE = de) { try { DE.RefreshCache(); // This will force credentials validation Console.WriteLine(""); Console.WriteLine("Exchange 2007 LDAP Validation successful"); Console.WriteLine(""); } catch (COMException ex) { Console.WriteLine(ex.Message); return false; } } } else { return false; throw new NullReferenceException(); } } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } public bool MailboxUserExists(string username) { DirectorySearcher deSearch = new DirectorySearcher(de); deSearch.ReferralChasing = ReferralChasingOption.All; deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))"; SearchResultCollection results = deSearch.FindAll(); //ERROR OCCURS try { if (results.Count > 0) { Console.WriteLine("User Found: " + results.Count); return true; } else { Console.WriteLine("User Not Found"); return false; } } catch (Exception ex) { throw new Exception(ex.Message); } } I've tried to take console output of my LDAP string I've passed and it is perfectly fine. Also using same LDAP connection string, I'm able to update AD fields for a particular user. I don't know why the code fails occassionaly. If there is no problem in code and if no solution is available, please provide any suggestions of handling these kind of active directory errors. Thanks in advance.brijeshvaidy...
Participant
848 Points
231 Posts
Re: Active Directory Error
Jun 30, 2012 07:50 AM|LINK
hello,
below link may help you..
http://forums.asp.net/t/1818001.aspx/1
http://msdn.microsoft.com/en-us/library/ms180881(v=vs.80).aspx
http://blogs.msdn.com/b/kaevans/archive/2011/07/04/querying-active-directory.aspx
http://www.codeproject.com/Articles/8488/Accessing-LDAP-User-list-using-VB-NET
Brijesh Vaidya
India
ASHU_TECHIT
0 Points
2 Posts
Re: Active Directory Error
Jun 30, 2012 08:24 AM|LINK
Hello,
Thanks for a quick response and the given links. I've refer to all the links and found no difference between the given code on the links and my own code. I've mentioned that my code works perfectly for most of the time. It's just sometime it throws these exceptions:- "a referral was returned from the server" or "An operation error occurred".
Can i manage these exceptions by making any changes to my existing code?? Please provide suggestions.
Thanks in advance.