您的位置:首页 > 编程语言 > ASP

ASP.NET登录和注册

2015-12-29 10:30 567 查看
原文地址: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>");
}
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: