Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my Login click button event code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;


namespace WebApplication1
{
    partial class ldapLogin : System.Web.UI.Page
    {
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            {
                // build UID string
                String uid = "uid=" + txtUsername.Text + ",ou=people,dc=example,dc=com";
                // assign password
                String password = txtPassword.Text;
                // define LDAP connection
                DirectoryEntry root = new DirectoryEntry(
                    "LDAP://directory.example.com", uid, password,
                    AuthenticationTypes.None);

                try
                {
                    // attempt to use LDAP connection
                    object connected = root.NativeObject;
                    // no exception, login successful
                    Response.Write("<span style=\"color:green;\">Login successful.</span>");
                }
                catch (Exception ex)
                {
                    // exception thrown, login failed
                    Response.Write("<span style=\"color:red;\">Login failed.</span>");
                }

                Response.Write("<p />");

            }
        }


    // page load event
    protected void Page_Load(object Sender, EventArgs e)
    {
        if (IsPostBack) {
            // form submitted, hide login form
            loginForm.Visible = false;
        } else {
            // first page load, show login form
            loginForm.Visible = true;}

    }
    }
}


This is my design code:

XML
<%@ Page Language="C#"
    AutoEventWireup="true"
    CodeFile="Default.aspx.cs"
    Inherits="ldapLogin"
%>
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void btnLogin_Click(object sender, EventArgs e)
    {

    }

    protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>LDAP Login</title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="loginForm" runat="server" visible="false">
            Username:
            <br />
            <br />
            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
            <p />
            Password:
            <br />
            <asp:TextBox TextMode="Password" ID="txtPassword" runat="server" />
            <p />
            <asp:Button ID="btnLogin" runat="server" Text="Login" onclick="btnLogin_Click" />
        </div>
    </form>
</body>
</html>


I am getting the below error when i am trying to execute :

Error 1 The name 'txtUsername' does not exist in the current context
Error 2 The name 'txtPassword' does not exist in the current context
Error 3 The name 'loginForm' does not exist in the current context

Please help what i am doing wrong ?
Posted
Updated 1-Feb-23 18:28pm
Comments
Suvendu Shekhar Giri 6-Nov-15 16:05pm    
Make sure that you are writing the code in the correct code behind file i.e, Default.aspx.cs
Member 12080232 6-Nov-15 16:09pm    
Do you have some sample code for how to authenticate user in asp.net using ldap through active directory
ZurdoDev 6-Nov-15 16:15pm    
The errors have nothing to do with Active Directory. You have a compilation issue that either your IDs do not match or your code behind does not match your aspx page.

Nothing found any error. Every things is fine.
Just change your code behind IDs (txtUsername)

Previous code
C#
protected void btnLogin_Click(object sender, EventArgs e)
   {
       {
           // build UID string
           String uid = "uid=" + txtUsername.Text + ",ou=people,dc=example,dc=com";

Current code
C#
protected void btnLogin_Click(object sender, EventArgs e)
   {
       {
           // build UID string
           String uid = "uid=" + txtUserName.Text + ",ou=people,dc=example,dc=com";


and
HTML
why are your write inline same cs code, where you used separate cs file
 
Share this answer
 
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="add_your_solution_name.ldapLogin"
%>
get project aspx file tag to this form
 
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