using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.DirectoryServices;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string dominName = string.Empty;
string adPath = string.Empty;
string userName = txtLoginID.Text.Trim().ToUpper();
string strError = string.Empty;
try
{
foreach (string key in ConfigurationSettings.AppSettings.Keys)
{
dominName = key.Contains("DirectoryDomain") ? ConfigurationSettings.AppSettings[key] : dominName;
adPath = key.Contains("DirectoryPath") ? ConfigurationSettings.AppSettings[key] : adPath;
if (!String.IsNullOrEmpty(dominName) && !String.IsNullOrEmpty(adPath))
{
if (true == AuthenticateUser(dominName, userName, txtPassword.Text, adPath, out strError))
{
Response.Redirect("default.aspx");
}
dominName = string.Empty;
adPath = string.Empty;
if (String.IsNullOrEmpty(strError)) break;
}
}
if (!string.IsNullOrEmpty(strError))
{
lblError.Text = "Invalid user name or Password!";
}
}
catch
{
}
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
{
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;
}
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;
}
}
}
source code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>LDAP Authentication</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<table cellpadding="1" cellspacing="1" style="background-color: #E0E0E0; border: 1px solid #727272">
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLoginID" Width="150" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblpwd" runat="server" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPassword" Width="150" TextMode="Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblError" runat="server" ForeColor="Red" Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
error is
Error 12 The type or namespace name 'SearchResult' could not be found (are you missing a using directive or an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 76 17 WebApplication4
Error 1 The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 14 14 WebApplication4
Error 10 The type or namespace name 'DirectorySearcher' could not be found (are you missing a using directive or an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 73 17 WebApplication4
Error 11 The type or namespace name 'DirectorySearcher' could not be found (are you missing a using directive or an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 73 48 WebApplication4
Error 8 The type or namespace name 'DirectoryEntry' could not be found (are you missing a using directive or an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 68 13 WebApplication4
Error 9 The type or namespace name 'DirectoryEntry' could not be found (are you missing a using directive or an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 68 40 WebApplication4
Error 3 The name 'txtPassword' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 39 75 WebApplication4
Error 14 The name 'txtPassword' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 97 13 WebApplication4
Error 2 The name 'txtLoginID' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 29 31 WebApplication4
Error 13 The name 'txtLoginID' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 96 13 WebApplication4
Error 4 The name 'lblError' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 51 21 WebApplication4
Warning 5 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings"' c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 33 40 WebApplication4
Warning 6 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings"' c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 35 67 WebApplication4
Warning 7 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings"' c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 36 62 WebApplication4