Hi,
I am trying to use login form with active directory authentication, when i type a correct username and password it works but too slow to load the next page.
But when i type a wrong username or password and click on sender button it stay loading the page trying to connect or something i dont know.
Heres the code:
protected void btnLogin_Click(object sender, EventArgs e)
{
string dominName = string.Empty;
string adPath = string.Empty;
string userName = txtLoginID.Text;
string strError = string.Empty;
try
{
foreach (string key in ConfigurationManager.AppSettings.Keys)
{
dominName = key.Contains("DirectoryDomain") ? ConfigurationManager.AppSettings[key] : dominName;
adPath = key.Contains("DirectoryPath") ? ConfigurationManager.AppSettings[key] : adPath;
if (!String.IsNullOrEmpty(dominName) && !String.IsNullOrEmpty(adPath))
{
if (true == AuthenticateUser(dominName, userName, txtPassword.Text, adPath, out strError))
{
Response.Redirect("/Metro-UI-CSS-master/HomePage.aspx"); // Authenticated user redirects to WebFormslide.aspx
}
dominName = string.Empty;
adPath = string.Empty;
if (String.IsNullOrEmpty(strError)) break;
}
}
if (!string.IsNullOrEmpty(strError))
{
lblError.Text = "Invalid user name or Password!";
}
}
catch (Exception ex)
{
}
finally
{
}
}
public bool AuthenticateUser(string domain, string username, string password, string LdapPath, out string Errmsg)
{
Errmsg = "";
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password);
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("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
// Update the new path to the user in the directory
LdapPath = result.Path;
string _filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
Errmsg = ex.Message;
return false;
throw new Exception("Error authenticating user." + ex.Message);
}
return true;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
txtLoginID.Text = string.Empty;
txtPassword.Text = string.Empty;
}
<appSettings>
<add key="DirectoryPath" value="LDAP://DEV-SRV-DOM"></add>
<add key="DirectoryDomain" value="DEVELCOM-SRV"></add>
</appSettings>
Can you help me with this issue?