在ASP.NET 中实现单点登录
2006-09-20 20:43
337 查看
在ASP.NET 中实现单点登录
SingleLogin.aspx代码
<%@ Page language="c#" Codebehind="SingleLogin.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cs" AutoEventWireup="false"
Inherits="eMeng.Exam.SingleLogin" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>单点登录测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Author" content="孟子E章">
<meta http-equiv="WebSite" content="http://dotnet.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cc/">
<style>
H3 { FONT: 17px 宋体 }
INPUT { FONT: 12px 宋体 }
SPAN { FONT: 12px 宋体 }
P { FONT: 12px 宋体 }
H4 { FONT: 12px 宋体 }
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div align="center">
<h3>单点登录测试</h3>
<p>用户名称:<<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox id="UserName" runat="server"></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox></p>
<p>用户密码:<<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox id="PassWord" runat="server" TextMode="Password"></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox></p>
<p><<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Button id="Login" runat="server" Text=" 登 录 "></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Button></p>
<p><<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Label id="Msg" runat="server"></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Label></p>
</div>
</form>
</body>
</HTML>
SingleLogin.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 eMeng.Exam
{
/// <summary>
/// SingleLogin 的摘要说明。
/// 实现单点登录
/// </summary>
public class SingleLogin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox UserName;
protected System.Web.UI.WebControls.TextBox PassWord;
protected System.Web.UI.WebControls.Label Msg;
protected System.Web.UI.WebControls.Button Login;
private void Page_Load(object sender, System.EventArgs e)
{
// 实际例子可访问:
// http://dotnet.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cc/Exam/SingleLogin.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </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://dotnet.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cc/'>【孟宪会之精彩世界】";
Msg.Text += "</a>,祝您浏览愉快!:)</h4>";
}
else
{
// 在 Cache 中发现该用户的记录,表名已经登录过,禁止再次登录
Msg.Text="<h4 style='color:red'>抱歉,您好像已经登录了呀:-(</h4>";
return;
}
}
}
}
SingleLogin.aspx代码
<%@ Page language="c#" Codebehind="SingleLogin.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cs" AutoEventWireup="false"
Inherits="eMeng.Exam.SingleLogin" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>单点登录测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Author" content="孟子E章">
<meta http-equiv="WebSite" content="http://dotnet.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cc/">
<style>
H3 { FONT: 17px 宋体 }
INPUT { FONT: 12px 宋体 }
SPAN { FONT: 12px 宋体 }
P { FONT: 12px 宋体 }
H4 { FONT: 12px 宋体 }
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div align="center">
<h3>单点登录测试</h3>
<p>用户名称:<<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox id="UserName" runat="server"></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox></p>
<p>用户密码:<<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox id="PassWord" runat="server" TextMode="Password"></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:TextBox></p>
<p><<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Button id="Login" runat="server" Text=" 登 录 "></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Button></p>
<p><<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Label id="Msg" runat="server"></<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>:Label></p>
</div>
</form>
</body>
</HTML>
SingleLogin.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 eMeng.Exam
{
/// <summary>
/// SingleLogin 的摘要说明。
/// 实现单点登录
/// </summary>
public class SingleLogin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox UserName;
protected System.Web.UI.WebControls.TextBox PassWord;
protected System.Web.UI.WebControls.Label Msg;
protected System.Web.UI.WebControls.Button Login;
private void Page_Load(object sender, System.EventArgs e)
{
// 实际例子可访问:
// http://dotnet.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cc/Exam/SingleLogin.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </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://dotnet.<a href="http://www.myfaq.com.cn/Dev/Web/Asp/index.html" target="_blank">asp</a>x.cc/'>【孟宪会之精彩世界】";
Msg.Text += "</a>,祝您浏览愉快!:)</h4>";
}
else
{
// 在 Cache 中发现该用户的记录,表名已经登录过,禁止再次登录
Msg.Text="<h4 style='color:red'>抱歉,您好像已经登录了呀:-(</h4>";
return;
}
}
}
}
相关文章推荐
- asp.net 多子域名实现单点登录
- ASP.Net课堂--实验1(登录注册页面实现)
- 在ASP.NET MVC中实现登录后回到原先的界面
- RSA实现登录页面密码加密(ASP.NET MVC)
- asp.net QQ 登录的实现
- ASP.NET中如何实现FORM认证登录
- ASP .NET MVC 微信授权获取用户信息并实现登录
- ASP.NET 安全认证(四)——巧妙实现 Form 表单认证跨站点、跨服务器的单点登录(Single Sign On) 作者:寒羽枫
- ASP.NET网络上实现单点登录
- asp.net 真正实现完全跨域单点登录
- ASP.NET中如何实现FORM认证登录
- asp.net 实现用户登录和注册——基于webform模式
- ASP.net实现单点登录
- ASP.NET中如何实现FORM验证登录
- ASP.net 实现禁止用户重复登录
- 单点登录在ASP.NET上的简单实现
- 在ASP.NET 中实现单点登录 出自:【孟宪会之精彩世界】
- asp.net 多子域名实现单点登录
- asp.net利用jquery ajax实现免刷新登录