asp.net 中用DropDownList实现用户身份验证登陆

作者在 2012-05-26 12:03:14 发布以下内容
本实例介绍有DropDownList空间实现根据不同的用户身份级别读取数据库中不同的表:数据库设置如下(当然你也可以根据你的需要设置)
普通用户表和管理员表如下
在vs2008新建一个网站在Default.aspx添加两个TextBox控件和一个DropDownList控件一个Button控件如下图
双击button进入代码页(代码如下)
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "用户登录";
        username.Focus();
        if (!IsPostBack)
        {
            DropDownList1.Items.Add("管理员");
            DropDownList1.Items.Add("普通用户");

        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {

        if (username.Text == "" || userpwd.Text == "")
        {
            Response.Write("<script language=javascript>alert('用户名或密码不能为空!!');</script>");
            return;

        }
        if (DropDownList1.SelectedItem.Text == "管理员")
        {
            string sqlstr = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            using (SqlConnection conn = new SqlConnection(sqlstr))
            {
                conn.Open();
                string AdminID = username.Text;
                string AdminPwd = userpwd.Text;
                string sql = "select * from adminlogin where AdminID='" + username.Text + "'and AdminPwd='" + userpwd.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dt = cmd.ExecuteReader();

                if (dt.Read())
                {
                    Session["AdminID"] = username.Text;
                    Session["AdminPwd"] = dt["AdminPwd"].ToString();
                    Response.Redirect("resties.aspx?name=" + username.Text);

                }
                else
                {
                    Response.Write("<script language=javascript>alert('用户名或密码不正确!!!');</script>");


                }
            }
        }
        if (DropDownList1.SelectedItem.Text == "普通用户")
        {
            string sqlst = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
            using (SqlConnection con = new SqlConnection(sqlst))
            {
                con.Open();
                string UserName = username.Text;
                string pwd = userpwd.Text;
                string sqlstl = "select * from login where UserName='" + username.Text + "'and pwd='" + userpwd.Text + "'";
                SqlCommand com = new SqlCommand(sqlstl, con);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    Session["UserName"] = username.Text;
                    Session["pwd"] = dr["pwd"].ToString();
                    Response.Redirect("resties.aspx?name=" + username.Text);

                }
                else
                {

                    Response.Write("<script language=javascript>alert('用户名或密码不正确!!');</script>");
                }
            }
        }
    }
}

默认分类 | 阅读 1678 次
文章评论,共0条
游客请输入验证码
文章分类