ASP.NET - Active directory Problem - Asked By mostafa hamdy on 19-Mar-12 05:54 AM

Hello all
I have some web site built in asp.net 4.0 , and I create some login page and authenticate user to active directory with the following method:

public bool LoginToActiveDirectory()

{


string strLDAPServer = "192.168.1.11";

string UserName = "test1";

string Password = "test123";

DirectoryEntry Entry = new DirectoryEntry("LDAP://" + strLDAPServer , UserName, Password);

try

{


string GetNameOfuser = Entry.Parent.Name;

object ent = Entry.NativeObject;

DirectoryEntry User = Entry.Children.Find("cn=" + UserName);

object members = User.Invoke("Groups", null);

foreach (object member in (System.Collections.IEnumerable)members)

{


DirectoryEntry login = new DirectoryEntry(member);

if (login.Name == "CN=Student")

{

Entry.Close();

Entry.Dispose();


return true;

}

}

 

 

Entry.Close();

Entry.Dispose();


return false;

}


catch(Exception ex)

{


string ss = ex.Message;

Entry.Close();

Entry.Dispose();


return false;

}


return true;

}
but I get the following exeception with me "A referral was returned from the server." when I try to test this method ,please if any body get what's this problem and can help me please send me or tell me about some URL may help me in solving this exception
Regards
Mostafa

kalpana aparnathi replied to mostafa hamdy on 19-Mar-12 06:09 AM
hi,

Try another way for this:

try
  {
    string adServer = ConfigurationManager.AppSettings["Server"];
    string adDomain = ConfigurationManager.AppSettings["Domain"];
    string adUsername = ConfigurationManager.AppSettings["AdiminUsername"];
    string password = ConfigurationManager.AppSettings["Password"];
    string[] dc = adDomain.Split('.');
    string dcAdDomain = string.Empty;
 
    foreach (string item in dc)
    {
      if (dc[dc.Length - 1].Equals(item))
        dcAdDomain = dcAdDomain + "DC=" + item;
      else
        dcAdDomain = dcAdDomain + "DC=" + item + ",";
    }
 
    DirectoryEntry de = new DirectoryEntry("LDAP://" + adServer + "/CN=Users," + dcAdDomain, adUsername, password);
 
    DirectorySearcher ds = new DirectorySearcher(de);
 
    ds.SearchScope = SearchScope.Subtree;
 
    ds.Filter = "(&(objectClass=User)(sAMAccountName=" + username + "))";
 
    if (ds.FindOne() != null)
      return true;
  }
  catch (Exception ex)
  {
    ExLog(ex);
  }
  return false;
}

Regards,
Jitendra Faye replied to mostafa hamdy on 19-Mar-12 06:30 AM
It may be possible that the path you supplied was not correct.

You may also need to enable ReferralChasing on the DirectorySearcher - http://msdn.microsoft.com/en-us/library/ms180884%28VS.80%29.aspx.
mostafa hamdy replied to kalpana aparnathi on 19-Mar-12 07:10 AM
Hello all , Hello kalpana
I did the code which you post me with the replacment with the server name , Domain, administrator and the Password
but still get the same error message which I got so far which is "A referral was returned from the server." please if any body get this error message so far and can help me please send me or tell me about some URL can help me
regards
Mostafa
Somesh Yadav replied to mostafa hamdy on 19-Mar-12 07:48 AM

username should always be specified as domain\username. Maybe that's the problem?

You can download
 http://download.softerra.com/files/ldapbrowser26.msi
for free to check if your connection works. You can also try your queries there.