📄 mysinglelogin.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 Example_12_13
{
/// <summary>
/// Summary description for MySingleLogin.
/// </summary>
public class MySingleLogin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox UserName;
protected System.Web.UI.WebControls.TextBox PassWord;
protected System.Web.UI.WebControls.Button Login;
protected System.Web.UI.WebControls.Label Msg;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Login.Click += new System.EventHandler(this.Login_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Login_Click(object sender, System.EventArgs e)
{
// 作为唯一标识的Key,应该是唯一的,这可根据需要自己设定规则。
// 做为测试,这里用用户名和密码的组合来做标识;也不进行其它的错误检查。
// 生成Key
string sKey = UserName.Text + "_" + PassWord.Text;
// 得到Cache中的给定Key的值
string sUser = Convert.ToString(Cache[sKey]);
// 检查是否存在
if (sUser == null || sUser == String.Empty)
{
// Cache中没有该Key的项目,表名用户没有登录,或者已经登录超时
// 注意下面使用的TimeSpan构造函数重载版本的方法,是进行是否登录判断的关键。
TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable,null);
Session["User"] = sKey;
// 首次登录,
Msg.Text="<h4 style='color:red'>嗨!欢迎您访问<a href='http://xtu991205.cendata.net.cn'>湘大计算机99级五班";
Msg.Text += "</a>,祝您浏览愉快!:)</h4>";
}
else
{
// 在 Cache 中发现该用户的记录,表名已经登录过,禁止再次登录
Msg.Text="<h4 style='color:red'>抱歉,您好像已经登录了呀:-(</h4>";
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -