C#登录检查代码无法正常工作 [英] C# login check code not working correctly

查看:64
本文介绍了C#登录检查代码无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为用户创建一个登录页面,并检查他们是管理员还是普通用户。我写了以下代码。我认为逻辑是正确的,但我不知道为什么程序的输出不正确。请帮我这个。



使用System;

使用System.Collections.Generic;

使用System .Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Configuration;

使用System.Data;

使用System.Data.SqlClient;



命名空间Bidders_Joint

{

公共部分类WebForm2:System.Web.UI.Page

{

string constr = ConfigurationManager.ConnectionStrings [BiddersJoint]。ToString();

字符串密码;

字符串类型;

protected void Page_Load(object sender,EventArgs e)

{



}



protected void btnLogin_Click(object sender,EventArgs e)

{







SqlConnectio n con = new SqlConnection(constr);

SqlCommand cmd = new SqlCommand(选择密码,从TABLE_USER输入User_ID = @ userid,con);

cmd。 Parameters.AddWithValue(@ userid,txtUserid.Text.ToString());

尝试

{

cmd.Connection.Open( );

SqlDataReader dr = cmd.ExecuteReader();

while(dr.Read())

{

password = dr [Password]。ToString();

type = dr [Type]。ToString();

}

if((password == txtPassword.Text.ToString())&&(type ==admin))

{

Response.Redirect(administrator .aspx);

}



if((password == txtPassword.Text.ToString())&&(type ==一般))

{

Response.Redirect(userspage.aspx);

}

else

{

lblMessage.Text =用户名或密码错误;

}





}

catch(例外情况)

{



lblMessage.Text = ex.Message;

}

终于

{

cmd.Connection.Close();

}

}









}

}



即使用户名和密码正确,输出仍然是用户ID或密码

I want to create a loginpage for user and check whether they are admin or general user. i have written the following code. I think the logic is correct but I dont know why the output of the program is not correct. Please help me with this.

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
{
string constr = ConfigurationManager.ConnectionStrings["BiddersJoint"].ToString();
string password;
string type;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnLogin_Click(object sender, EventArgs e)
{



SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("select Password,Type from TABLE_USER where User_ID = @userid", con);
cmd.Parameters.AddWithValue("@userid",txtUserid.Text.ToString());
try
{
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
password = dr["Password"].ToString();
type = dr["Type"].ToString();
}
if ((password == txtPassword.Text.ToString()) && (type == "admin"))
{
Response.Redirect("administrator.aspx");
}

if ((password==txtPassword.Text.ToString()) && (type=="general"))
{
Response.Redirect("userspage.aspx");
}
else
{
lblMessage.Text = "wrong userid or password";
}


}
catch (Exception ex)
{

lblMessage.Text = ex.Message;
}
finally
{
cmd.Connection.Close();
}
}




}
}

output is always worng userid or password even when userid and password are correct

推荐答案

将代码更改为此...实际上您的逻辑是正确的但方法是错误的。

通常的方法是



Change the code to this... actually your logic is correct but approach is wrong.
The usual approach is

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);
cmd.Parameters.AddWithValue("@password",txtPassword.Text);
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");
  }
 else  if (type=="general")
  {
       Response.Redirect("userspage.aspx");
  }
}

else
{
lblMessage.Text = "wrong userid or password";
}
}
 
}
catch (Exception ex)
{
 lblMessage.Text = ex.Message;
}
finally
{
con.close(); //cmd.Connection.Close();
}
}





我已将您声明的变量放在Click事件中并更改了查询。



I have put the variables you declared inside the Click event and Changed the query.


这篇关于C#登录检查代码无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆