ASP.NET登陆和注册

原文地址:ASP.NET用户登陆和注册的代码做者:54黄二
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
    protected System.Data.SqlClient.SqlConnection Cn;
    protected System.Data.SqlClient.SqlCommand Cm;
    protected System.Data.SqlClient.SqlDataAdapter Da;
    protected System.Data.DataSet Ds;
    protected System.Data.SqlClient.SqlDataReader Dr;
    protected void Regist_Click(object sender, EventArgs e)
    {
        string str = ConfigurationSettings.AppSettings["strConnection"];
        Cn = new SqlConnection(str);
        Cn.Open();
        Cm = new SqlCommand("SELECT * FROM userlogin WHERE username='" + nametex.Text + "'", Cn);
        Dr = Cm.ExecuteReader();

        if (Dr.Read())//若是存在相同用户名
        {
            Response.Write("<script>alert('用户已被注册');window.window.location.href='Login.aspx';</script>") ;
            Dr.Close();
        }
        else
        {
            Dr.Close();
            SqlCommand Cm2 = new SqlCommand("INSERT INTO userlogin (username,password,email,question,answer) VALUES ('" + nametex.Text + "','" + passwtex.Text + "','" + mailtex.Text + "','" + questex.Text + "','" + anstex.Text + "')", Cn);
            int i = Cm2.ExecuteNonQuery();
            //message.InnerHtml = "注册成功";
            Response.Write("<script>alert('注册成功');window.window.location.href='Login.aspx';</script>");
        }
        Cn.Close();
    }
    protected void Login_Click(object sender, EventArgs e)
    {
        if (us.Text!= null && pas.Text != null)
        {
            string str = ConfigurationSettings.AppSettings["strConnection"];
            Cn = new SqlConnection(str);
            Cn.Open();
            Cm = new SqlCommand("SELECT * FROM userlogin WHERE username='" + us.Text + "' AND password ='" + pas.Text + "'", Cn);
            Dr = Cm.ExecuteReader();
            if (Dr.Read())//用户名和密码是否正确
            {
                Session["username"] = us.Text;
                Session["password"] = pas.Text;
                //FormsAuthentication.SetAuthCookie(userTxt.Text, PersistCookie.Checked);
                // FormsAuthentication.RedirectFromLoginPage(userTxt.Text, PersistCookie.Checked);
                Response.Write("<script>alert('登录成功');window.window.location.href='Login.aspx';</script>");
                Dr.Close();

            }
            else
            {
                //Dr.Close();
                //message.InnerHtml = "用户名或密码错误!若是还未注册,请先注册!";
                Response.Write("<script>alert('用户名或密码错误!若是还未注册,请先注册!');window.window.location.href='Login.aspx';</script>");
            }
            Cn.Close();
        }
        else
        {
            Response.Write("<script>alert('请输入用户名和密码!');window.window.location.href='Login.aspx';</script>");
        }
    }
  
}