我正在尝试使用C#中的ldap对用户进行身份验证,但会出现以下错误。

问题描述:

这是我的登录点击按钮事件代码:



This is my Login click button event code:

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:

<%@ 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>





当我尝试执行时,我收到以下错误:



错误1名称'txtUsername'd oes当前上下文中不存在

错误2当前上下文中不存在名称'txtPassword'

错误3当前名称'loginForm'不存在上下文



请帮助我做错了什么?



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 ?

没什么发现任何错误。一切都很好。

只需更改ID后面的代码( txtUsername



以前的代码

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

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



当前代码


Current code

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









and

why are your write inline same cs code, where you used separate cs file