⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 login.aspx.cs

📁 通过asp.net实现普通的聊天工具 内容比较简单
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace liaotian
{
	/// <summary>
	/// login 的摘要说明。
	/// </summary>
	public class login : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtName;
		protected System.Web.UI.WebControls.TextBox txtPassword;
		protected System.Web.UI.WebControls.Button btnSubmit;
		protected System.Web.UI.WebControls.Label lblMsg;
		protected System.Web.UI.WebControls.LinkButton lkbForPassowrd;
		protected System.Web.UI.HtmlControls.HtmlTable Table1;
		protected System.Web.UI.WebControls.Panel plGetPassword;
		protected System.Web.UI.WebControls.Button btnGetPassword;
		protected System.Web.UI.WebControls.Panel plLogin;
		protected System.Web.UI.WebControls.TextBox txtGetName;
		protected System.Web.UI.WebControls.TextBox txtGetEmail;
		protected System.Web.UI.WebControls.TextBox txtRegName;
		protected System.Web.UI.WebControls.TextBox txtRegPwdSure;
		protected System.Web.UI.WebControls.TextBox txtRegEmail;
		protected System.Web.UI.WebControls.RequiredFieldValidator rvfRegName;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvRegPassword;
		protected System.Web.UI.WebControls.CompareValidator cvRegPassword;
		protected System.Web.UI.WebControls.RegularExpressionValidator revRegEmail;
		protected System.Web.UI.WebControls.Button btnRegSubmit;
		protected System.Web.UI.HtmlControls.HtmlInputButton Reset1;
		protected System.Web.UI.WebControls.Button btnReg;
		protected System.Web.UI.WebControls.Panel plReg;
		protected System.Web.UI.WebControls.Label lblRegMsg;
		protected System.Web.UI.WebControls.TextBox txtRegPassword;
		protected System.Web.UI.WebControls.CheckBox ckbCookie;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(!this.IsPostBack)
			{
				if(Request.Cookies["user"]!=null)
				{
					Response.Redirect("mainFrame.htm");
				}
			}

			plLogin.Visible = true;
			plGetPassword.Visible = false;
			plReg.Visible = false;

			
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
			this.btnReg.Click += new System.EventHandler(this.btnReg_Click);
			this.lkbForPassowrd.Click += new System.EventHandler(this.lkbForPassowrd_Click);
			this.btnGetPassword.Click += new System.EventHandler(this.btnGetPassword_Click);
			this.btnRegSubmit.Click += new System.EventHandler(this.btnRegSubmit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSubmit_Click(object sender, System.EventArgs e)
		{
			UserInfo user = new UserInfo();
			user.Name = txtName.Text.ToString();
			user.Password = txtPassword.Text;

			ArrayList al = Application["userList"] as ArrayList;
			foreach(string name in al)
			{
				if(name.Equals(txtName.Text.ToString ()))
				{
					lblMsg.Text = "此用户名己登陆过了!!!";
				}
				
			}
					DbSource source = new DbSource();
					if(source.getByName(user.Name))
					{
						if(source.getByPassword(user))
						{
							//登陆成功后,设置cookie
							if(ckbCookie.Checked)
							{
								HttpCookie objCookie= new HttpCookie("user",user.Name);
								objCookie.Expires = DateTime.Now.AddMinutes(15);
								Response.Cookies.Add(objCookie);
							}
					
							//设置sessionName、userList
							ArrayList userList = Application["userList"] as ArrayList;
							userList.Add(user.Name);
					
					
							Session["userName"]=user.Name;


							Response.Redirect("mainFrame.htm");
						}
						else
						{
							lblMsg.Text = "密码错误,请重新输入";
						}
					}
					else
					{
						lblMsg.Text = "此用户名不存在,请您注册一个用户名";
					}
				
			}
		

		private  void lkbForPassowrd_Click(object sender, System.EventArgs e)
		{
			plLogin.Visible = false;
			plGetPassword.Visible = true;
		}

		//用户忘记密码时,通过用户名和Email取回密码
		private void btnGetPassword_Click(object sender, System.EventArgs e)
		{
			plLogin.Visible = true;
			plGetPassword.Visible = false;

			UserInfo user = new UserInfo();
			user.Name = txtGetName.Text;
			user.Email =txtGetEmail.Text;

			DbSource source = new DbSource();
			string password = source.getByEmail(user);
			if(password.Equals (""))
			{
			}
			else
			{
				lblMsg.Text = "密码为:" + password;
			}
			
		}

		private void btnReg_Click(object sender, System.EventArgs e)
		{
			plLogin.Visible = false;
			plGetPassword.Visible = false;
			plReg.Visible = true;			
		
		}

		private void btnRegSubmit_Click(object sender, System.EventArgs e)
		{
			UserInfo user = new UserInfo();

			user.Name = txtRegName.Text.ToString();			
			user.Email = txtRegEmail.Text .ToString ();
			user.Password = txtRegPassword.Text.ToString();

			lblMsg.Text = user.Name + user.Password + user.Email;

			DbSource source = new DbSource();
			if(source.createUser(user))
			{
				lblRegMsg.Text = "注册成功";
			}
			else
			{
				lblRegMsg.Text = "注册失败";
			}
		}

	

		

		
		

	}

	public class DbSource
	{
		

		//验证用户名是否存在
		public bool getByName(string userName)
		{
			
			string dbPath = HttpContext.Current.Server.MapPath("DataSource/userInfo.mdb");
			string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
			string sql = "select * from userInfo where [name]='" + userName +"'";
			OleDbConnection olconn = new OleDbConnection(conn);			
			olconn.Open();
			OleDbCommand olcomm = olconn.CreateCommand();
			olcomm.CommandText = sql;
			if(olcomm.ExecuteScalar()!=null)
			{
				olconn.Close();
				return true;
			}else
			{
				olconn.Close();
				return false;
			}
			
		}

		//验证用户密码是否正确
		public bool getByPassword(UserInfo user)
		{
			string dbPath = HttpContext.Current.Server.MapPath("DataSource/userInfo.mdb");
			string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
			string sql="select [password] from userInfo where [name]='" + user.Name + "' and [password]='" + user.Password + "'";
			OleDbConnection oleConn = new OleDbConnection(conn);			
			oleConn.Open();
			OleDbCommand oleComm = oleConn.CreateCommand();
			oleComm.CommandText=sql;
			if(oleComm.ExecuteScalar()!=null)
			{
				oleConn.Close();
				return true;
			}
			else
			{
				oleConn.Close();
				return false;
			}
		}
		
		//忘记密码的用户通过Email取得密码
		public string getByEmail(UserInfo user)
		{

			string dbPath = HttpContext.Current.Server.MapPath("DataSource/userInfo.mdb");
			string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
			string sql="select [password] from userInfo where [name]='" + user.Name + "' and Email='" +user.Email + "'";
			string password="";
			OleDbDataReader reader;
			OleDbConnection oleConn= new OleDbConnection(conn);
			
			oleConn.Open();
			OleDbCommand oleComm = oleConn.CreateCommand();
			oleComm.CommandText = sql;
			reader = oleComm.ExecuteReader();
			while(reader.Read())
			{ 
				password = reader.GetString(0);
			}
			reader.Close();
			oleConn.Close();
			if(password.Equals(""))
			{
				return null;
			}
			else
			{
				return password;
			}
		
			

			
		}

		//注册用户,增加用户信息
		public bool createUser(UserInfo user)
		{
			string dbPath = HttpContext.Current.Server.MapPath("DataSource/userInfo.mdb");
			string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
			string sql = "insert into userInfo([name],[password],email) values ('" + user.Name + "','" +user.Password + "','" + user.Email + "')";
			OleDbConnection olconn = new OleDbConnection(conn);			
			olconn.Open();
			OleDbCommand olcomm = olconn.CreateCommand();
			olcomm.CommandText = sql;
			if(olcomm.ExecuteNonQuery()>=1)
			{
				olconn.Close();
				return true;
			}
			else
			{
				olconn.Close();
				return false;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -