我已经编写了登录检查和用户类型检查的C#代码.逻辑似乎是正确的,但为什么输出不正确

本文关键字:似乎是 不正确 输出 为什么 代码 登录 检查和 检查 类型 用户 | 更新日期: 2023-09-27 18:22:07

我已经编写了用于登录检查和用户类型检查的C#代码。逻辑似乎是正确的,但为什么输出不正确?

我在这里做了一些编辑。请现在检查一下。

没有进行重定向没有进行重定向没有发生重定向

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Bidders_Joint
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {

            string constr = ConfigurationManager.ConnectionStrings["BiddersJoint"].ToString();
            string type;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select Type from TABLE_USER where User_ID = @userid AND Password=@password", con);
            cmd.Parameters.AddWithValue("@userid", txtUserid.Text.ToString());
            cmd.Parameters.AddWithValue("@password", txtPassword.Text.ToString());
            try
            {
                con.Open();//cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.HasRows)
                    {
                        type = dr["Type"].ToString();
                        if (type == "admin")
                        {
                            Response.Redirect("administrator.aspx");
                            Response.End();
                        }
                        else if (type == "general")
                        {
                            Response.Redirect("userspage.aspx");
                            Response.End();
                        }
                    }
                    else
                    {
                        lblMessage.Text = "wrong userid or password";
                    }
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
            finally
            {
                con.Close(); //cmd.Connection.Close();
            }
        }
    }
}

我已经编写了登录检查和用户类型检查的C#代码.逻辑似乎是正确的,但为什么输出不正确

else:连接第一个和第二个if

while (dr.Read())
{
      password = dr["Password"].ToString();
      type = dr["Type"].ToString();
      if ((password == txtPassword.Text.ToString()) && (type == "admin"))
      {
         Response.Redirect("administrator.aspx");
      }
      else if ((password==txtPassword.Text.ToString()) && (type=="general"))
      {
         Response.Redirect("userspage.aspx");
      }
}
lblMessage.Text = "wrong userid or password";

更新:

                while (dr.Read())
                {
                        type = dr["Type"].ToString();
                        if (type == "admin")
                        {
                            Response.Redirect("administrator.aspx");
                            Response.End();
                        }
                        else if (type == "general")
                        {
                            Response.Redirect("userspage.aspx");
                            Response.End();
                        }
                }
                lblMessage.Text = "wrong userid or password";
相关文章: