gpt4 book ai didi

mysql - 验证在 login.cs 页面中不起作用

转载 作者:行者123 更新时间:2023-11-29 02:43:38 25 4
gpt4 key购买 nike

我在登录页面中创建了用户名和密码字段,并在 asp.net 页面中使用带有 Entity Framework 的 mysql 数据库。

这是我的 asp.net 代码:

<asp:TextBox ID="txtUsername" class="form-control" runat="server" placeholder="Username" required=""></asp:TextBox>
<asp:TextBox ID="txtPassword" class="form-control" TextMode="Password" runat="server" placeholder="Password" required=""></asp:TextBox>
<asp:Button ID="btnLogin" class="btn btn-primary block full-width m-b" runat="server" Text="Sign In" OnClick="btnSubmit_Click" />

下面是我的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
{

if (txtUsername.Text != null)
{
Response.Write("<script>alert('Your username is correct!!!')</script>");
}
else
{
Response.Write("<script>alert('Please enter the Username!!!')</script>");
}

if (txtPassword.Text != null)
{
Response.Write("<script>alert('Your password is correct!!!')</script>");
}
else
{
Response.Write("<script>alert('Please enter the password!!!')</script>");
}

contractmanagement_dbEntities dbEntities = new contractmanagement_dbEntities();
var UserName = txtUsername.Text;
var Password = txtPassword.Text;
var login = from M in dbEntities.adminlogins.Where(M => M.Username == UserName && M.UserPassword == Password) select M;

if (login.Count() > 0)
{
var username = txtUsername.Text;
Session["username"] = username;
Response.Redirect("index.aspx");
}
else
{
Response.Write("<script>alert('Username or password is not Valid!!!')</script>");
}
}

在后面的代码中,我为用户名和密码都设置了不等于空值,所以如果相应的用户名和密码为空?

最佳答案

您可以在登录页面顶部为消息添加隐藏标签,然后在后面的代码中可以将其文本设置为“您想要的消息”并使其可见,如下所示:

txtLabel.Text = "Your desired message";
txtLabel.Visible = true;

此外,当用户名和密码都不为空时,您必须将登录代码移动到 if block :

  if((txtUsername.text != null) && (txtPassword.Text != null))
{
contractmanagement_dbEntities dbEntities = new contractmanagement_dbEntities();
var UserName = txtUsername.Text;
var Password = txtPassword.Text;
var login = from M in dbEntities.adminlogins.Where(M => M.Username == UserName && M.UserPassword == Password) select M;

if (login.Count() > 0)
{
var username = txtUsername.Text;
Session["username"] = username;
Response.Redirect("index.aspx");
}
else
{
txtLabel.Text = "Invalid Username or password";
txtLabel.Visible = true;
}

}

关于mysql - 验证在 login.cs 页面中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46419902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com