自己试着做一个只有几个网页的简易网站。想要有用户注册、登陆,注销的功能。连接了数据库,已经实现了用户注册功能,并将用户注册的信息存入数据库。怎样做一个用户登录验证控件,在页面运行时验证输入的用户名和密码是否与数据库中已有的的用户信息匹配,匹配了则可成功登录?
我一直实现不了登录,下面是我的代码:
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");
    }
}

解决方案 »

  1.   

    可以使用Membership和Form验证实现,参考系统的Login控件的实现。
      

  2.   

    反编译 Login控件看看
    用户控件实现登录
    细说ASP.NET Forms身份认证
      

  3.   

    用 微软的自己的 login ,或只 用 html 自己做个 显示和隐藏。
      

  4.   

    if (TxtUserPwd.Text == user.Password) //TxtUserPwd.Text是登录时输入的密码
       {
       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("你要跳转到的另一个页面");
         }
    我就是这样做的
      

  5.   

    我修改过后大体上能验证通过,只不过还有一点异常不知道在哪里 ,当使用合法用户登录时,可以登陆进去,但是会跳转到catch语句里的界面,而输入错误用户信息时只显示“登陆失败”,不会跳转到catch里的页面,所以我想应该是if语句部分出错了,哪位可以指点一下
    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");
            }
      

  6.   

    使用 Login控件看看
      
      

  7.   

    给你推荐一本书吧:郭常圳、李云锦主编,ASP.NET网络应用开发例学与实践。里面关于ASP.NET有很详细的讲解,配备有两个小型软件的制作,你一定能找到你需要的东西。