自己试着做一个只有几个网页的简易网站。想要有用户注册、登陆,注销的功能。连接了数据库,已经实现了用户注册功能,并将用户注册的信息存入数据库。怎样做一个用户登录验证控件,在页面运行时验证输入的用户名和密码是否与数据库中已有的的用户信息匹配,匹配了则可成功登录?
我一直实现不了登录,下面是我的代码:
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.SqlClient;public partial class web_controls_userlogin : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] != null)
{
BsUser user = (BsUser)Session["user"];
Label1.Text = "欢迎"+ user.Username + "进入";
MultiView1.ActiveViewIndex = 1; //注销页面
}
else
{
MultiView1.ActiveViewIndex = 0; //登录界面
}
}
protected void LoginButton_Click1(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["db1"].ConnectionString;
string sql = "select username,password from userlogin where id=@id";
SqlConnection connection = new SqlConnection(constr);
SqlCommand command = new SqlCommand(sql,connection );
command.Parameters.Add(new SqlParameter("@id", TxtUserName.Text)); //TxtUserName.Text是登录时输入的ID
connection.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
BsUser user = new BsUser(); //BsUser是自定义的的用户类
user.Username = dr.GetString (0);
user .Password = dr.GetString (1);
if (TxtUserPwd.Text == user.Password) //TxtUserPwd.Text是登录时输入的密码
{
Session["user"] = user;
Response.Redirect("~/Default.aspx");
}
}
dr.Close();
connection.Close();
}
protected void LogoutButton_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("~/Default.aspx");
}
}
我一直实现不了登录,下面是我的代码:
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.SqlClient;public partial class web_controls_userlogin : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] != null)
{
BsUser user = (BsUser)Session["user"];
Label1.Text = "欢迎"+ user.Username + "进入";
MultiView1.ActiveViewIndex = 1; //注销页面
}
else
{
MultiView1.ActiveViewIndex = 0; //登录界面
}
}
protected void LoginButton_Click1(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["db1"].ConnectionString;
string sql = "select username,password from userlogin where id=@id";
SqlConnection connection = new SqlConnection(constr);
SqlCommand command = new SqlCommand(sql,connection );
command.Parameters.Add(new SqlParameter("@id", TxtUserName.Text)); //TxtUserName.Text是登录时输入的ID
connection.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
BsUser user = new BsUser(); //BsUser是自定义的的用户类
user.Username = dr.GetString (0);
user .Password = dr.GetString (1);
if (TxtUserPwd.Text == user.Password) //TxtUserPwd.Text是登录时输入的密码
{
Session["user"] = user;
Response.Redirect("~/Default.aspx");
}
}
dr.Close();
connection.Close();
}
protected void LogoutButton_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("~/Default.aspx");
}
}
用户控件实现登录
细说ASP.NET Forms身份认证
{
Session["user"] = user;
Response.Redirect("~/Default.aspx");
}
这样写不好吧,你最好在SQL里写个存储过程,把登录名和密码设为变量,返回值为0,1,存在返回1,不存在返回0,这样调用时,把登录名和密码的textbox里的值传入,将返回值赋给一个lable,然后才是:
if (lable1.Text == “1”)
{
Response.Redirect("你要跳转到的页面");
}
if (lable1.Text == “0”)
{
Response.Redirect("你要跳转到的另一个页面");
}
我就是这样做的
try
{
string constr = ConfigurationManager.ConnectionStrings["db1"].ConnectionString;
string sql = "select username from userlogin where id='" + TxtUserName.Text.Trim() + "' and password='" + TxtUserPwd.Text.Trim() + "'";
SqlConnection connection = new SqlConnection(constr);
//SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, connection); //创建适配器
DataSet ds = new DataSet(); //创建数据集
int count = da.Fill(ds, "table");
if (count > 0)
{
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
Session["user"] = dr["username"].ToString();
dr.Close();
Response.Redirect("~/Default.aspx");
}
else
{
Label2.Text = "登录失败";
}
connection.Close();
}
catch
{
Response.Redirect("~/web/errormessage/loginerror.aspx");
}