Click here to Skip to main content
15,885,876 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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");// Authenticated user redirects to 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
            {
                // 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;
        }
    }
}


source code


XML
<%@ 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" />
                        &nbsp;<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

ASM
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
Posted
Updated 25-Aug-14 21:38pm
v4
Comments
harshavardhan12345678 26-Aug-14 3:39am    
try in ur pc n reply

1 solution

On the .NET tab in the Add Reference dialog box, click System.DirectoryServices.dll, click Select, and then click OK.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900