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>"); } } }
相关文章推荐
- casperjs可以只抓取某个元素的截图
- JasperReport 中文问题解决
- casperjs的启动
- 【深入ASP.NET原理系列】--ASP.NET请求管道、应用程序生命周期、整体运行机制
- Asp导出数据库数据到Excel
- ASP.NET MVC实现仪表程序
- ASP.NET服务器控件开发(1)封装html
- aspx页面与ascx控件脚本冲突的问题
- asp.net站点阻止某个文件夹或者文件被浏览器访问
- asp.net get图
- 运用ASP.NET实现
- 树莓派 Raspberry Pi
- 【ASP.NET 进阶】根据IP地址返回对应位置信息
- ASP.NET请求管道、应用程序生命周期、整体运行机制
- ASP.NET中JSON的序列化和反序列化
- ASP.NET MVC 随想录
- .aspx 如何转.html
- asp.net mvc 、 ajax 批量上传文件
- ASP.NET 页面生命中的关键事件的执行顺序
- 最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂去吃翔