C# ASP.net名称';用户名';在当前上下文中不存在

C# ASP.net名称';用户名';在当前上下文中不存在,c#,asp.net,C#,Asp.net,我在不同的网站上点击了20多个链接试图解决这个问题 请帮帮我 所以我尝试做一个简单的登录验证 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Security; public partial class Account_Login :

我在不同的网站上点击了20多个链接试图解决这个问题

请帮帮我

所以我尝试做一个简单的登录验证

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void loginButton_Click(object sender, EventArgs e)
{
    if(FormsAuthentication.Authenticate(UserName.Text, Password.Text)
    {

    }
}
}
但会弹出一条错误消息,说明“名称‘userName’在当前上下文中不存在”,并且名称‘Password’在当前上下文中不存在

这是aspx代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"      CodeFile="Login.aspx.cs" Inherits="Account_Login" %>

 <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
<hgroup class="title">
    <h1>Login Page</h1>
</hgroup>
<section id="loginForm">
    <asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
        <LayoutTemplate>
            <p class="validation-summary-errors">
                <asp:Literal runat="server" ID="FailureText" />
            </p>
            <fieldset>
                <legend>Log in Form</legend>
                <ol>
                    <li>
                        <asp:Label ID="Label1" runat="server" AssociatedControlID="UserName">User name</asp:Label>
                        <asp:TextBox runat="server" ID="UserName" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                    </li>
                    <li>
                        <asp:Label ID="Label2" runat="server" AssociatedControlID="Password">Password</asp:Label>
                        <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                    </li>
                    <li>
                        <asp:CheckBox runat="server" ID="RememberMe" />
                        <asp:Label ID="Label3" runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
                    </li>
                </ol>
                <asp:Button ID="loginButton" runat="server" CommandName="Login" Text="Log in" OnClick="loginButton_Click" />
            </fieldset>
        </LayoutTemplate>
    </asp:Login>
    <p>
        <a href="Register.aspx">Register</a>
        if you don't have an account.
    </p>
</section>

如果你没有账户。

请帮助我:)

C#区分大小写。 根据您的控制ID,它应该是大写“U”的用户名:

<asp:TextBox runat="server" ID="UserName" />
编辑-注意到有问题的控件位于usercontrol内,因此代码现在应该是:

protected void loginButton_Click(object sender, EventArgs e)
{
    if(FormsAuthentication.RedirectFromLoginPage(Login1.UserName.Text, Password.Text)
    {

    }
}
但是,您的用户名ID仍然有一个输入错误

C#区分大小写。 根据您的控制ID,它应该是大写“U”的用户名:

<asp:TextBox runat="server" ID="UserName" />
编辑-注意到有问题的控件位于usercontrol内,因此代码现在应该是:

protected void loginButton_Click(object sender, EventArgs e)
{
    if(FormsAuthentication.RedirectFromLoginPage(Login1.UserName.Text, Password.Text)
    {

    }
}

但是,您的用户名ID中仍然存在输入错误。代码区分大小写

if(FormsAuthentication.RedirectFromLoginPage(UserName.Text, Password.Text)
{

}

代码区分大小写

if(FormsAuthentication.RedirectFromLoginPage(UserName.Text, Password.Text)
{

}

UserName位于Login控件内,因此您需要通过Login1控件访问它

string userName = Login1.UserName;
string password = Login1.Password;    
代码中的另一个问题是,如果使用Login控件,则不应使用Login按钮单击事件

取而代之的是,您需要根据您的饱腹感使用以下选项-

  • 登录
  • 洛格丁

用户名位于登录控件内,因此您需要通过登录1控件访问它

string userName = Login1.UserName;
string password = Login1.Password;    
代码中的另一个问题是,如果使用Login控件,则不应使用Login按钮单击事件

取而代之的是,您需要根据您的饱腹感使用以下选项-

  • 登录
  • 洛格丁

ID是用户名而不是用户名…哎呀,很抱歉我在测试驼峰大小写是否影响了它。但是它没有,而且忘记了更改值。但是我仍然得到相同的错误,谢谢:)ID是用户名而不是用户名…哎呀,很抱歉我在测试驼峰大小写是否影响了它。但是它没有,并且忘记了更改值。但是我仍然得到相同的error,谢谢:)UserName Textbox在登录控件内。您不能像那样访问它-UserName.Text。@Ahmedilya是的,我忘记将用户名更改为UserName,因为我正在测试骆驼壳是否影响它。但事实证明它没有影响它。(Login1.UserName.Text,Password.Text)<如果在参数中这样做,则用户名字段“string”的文本中会出现错误“string”不包含文本的定义,并且找不到接受“string”类型的第一个参数的扩展方法“Text”(是否缺少using指令或程序集引用?)感谢您的努力:)@Ahmedilyas and Win我已将其更改为身份验证,因为RedirectToLogin参数不允许同时包含两个字符串。因此我将其更改为身份验证字符串userName=Login1.userName;字符串password=Login1.password;if(FormsAuthentication.Authenticate(用户名,密码){}这就是我的代码现在的样子。但是我得到的警告“System.Web.Security.FormsAuthentication(string,string)”已经过时:“推荐的替代方法是使用会员API,例如Membership.ValidateUser谢谢:)userName文本框在登录控件内。您不能像那样访问它-UserName.Text@Ahmedilyas是的,我忘了将用户名改为用户名,因为我正在测试骆驼套是否会影响它。但事实证明并非如此。(Login1.UserName.Text,Password.Text)<如果我在参数内这样做,我会在用户名字段'string'的文本中得到错误,因为它不包含Text'的定义,并且找不到接受'string'类型的第一个参数的扩展方法'Text'(是否缺少using指令或程序集引用?)感谢您的努力:)@Ahmedilyas and Win我已将其更改为“身份验证”,因为RedirectToLogin参数不允许其中同时包含两个字符串。所以我将其更改为Authenticate string userName=Login1.userName;字符串密码=Login1.密码;if(FormsAuthentication.Authenticate(userName,password)){}这就是我的代码现在的样子。但是我得到的警告“System.Web.Security.FormsAuthentication(string,string)”已经过时:“推荐的替代方法是使用会员API,例如Membership.ValidateUser谢谢:)Hey Win:)我已经尝试过了string userName=Login1.userName;string password=Login1.password;如果(FormsAuthentication.RedirectFromLoginPage(用户名,密码)){}但我收到错误消息
if(FormsAuthentication.RedirectFromLoginPage(用户名,密码)){
是无效语句。RedirectFromLoginPage的返回类型为空,因此它不返回任何内容。请参阅我的评论。@helloGoodDay根据您在其他答案中的评论,您是在尝试实现表单身份验证还是成员资格提供程序?表单身份验证是成员资格提供程序的子集。无论哪种方式,它们都完全不同从您的原始问题中删除;它们是一个巨大的主题,无法在评论区中回答。如果您有关于-表单身份验证或成员资格提供商的问题,请创建一个单独的问题。您是对的:)我将其更改为表单身份验证。身份验证(用户名、密码))现在它可以工作了。非常感谢。我正在尝试实现一个表单身份验证Hey Win:)我已经用这种方式尝试了string userName=Login1.userName;string password=Login1.password;if(FormsAuthentication.RedirectFromLoginPage(userName,password)){}但是我得到了错误