惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
博客园 - 叶小钗
T
Tailwind CSS Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Exploit Database - CXSecurity.com
Blog — PlanetScale
Blog — PlanetScale
T
Tenable Blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
A
Arctic Wolf
P
Proofpoint News Feed
S
SegmentFault 最新的问题
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Y
Y Combinator Blog
WordPress大学
WordPress大学
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
AWS News Blog
AWS News Blog
小众软件
小众软件
U
Unit 42
云风的 BLOG
云风的 BLOG
美团技术团队
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
L
LangChain Blog
G
GRAHAM CLULEY
P
Proofpoint News Feed
I
Intezer
Security Latest
Security Latest
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
Cisco Talos Blog
Cisco Talos Blog
T
The Blog of Author Tim Ferriss
H
Heimdal Security Blog
T
Tor Project blog
MongoDB | Blog
MongoDB | Blog
The Cloudflare Blog
T
Troy Hunt's Blog
Know Your Adversary
Know Your Adversary
博客园 - 司徒正美
Google Online Security Blog
Google Online Security Blog
V
V2EX
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 绯村剑心

日期对象ToString方法格式符的大小写对输出结果是有影响的 - 绯村剑心 - 博客园 Ajax基础配置 — XMLHttpRequest 让文本输入框只能输入数字 - 绯村剑心 - 博客园 抓取网页中的链接 检测客户端显示器分辨率、浏览器类型和客户端IP 对于长时间装载的ASP.NET页面如何在客户端浏览器中显示进度? 带图片的,多列的DropDownList的实现 - 绯村剑心 - 博客园 一个很不错介绍session的文章 无法从Web服务器获取项目文件 曼彻斯特码与差分曼彻斯特码 如何清除SQL server日志 sql server日志文件总结及日志满的处理办法 SQL SERVER 安装问题详解 Server实用操作小技巧集合 从html中提出纯文本 用JAVASCRIPT来刷新框架子页面的七种方法。 创建高度动态变化的Iframe 上传图片并生成缩略图 ASP.NET程序中常用的三十三种代码
在ASP.NET 中实现单点登录(利用Cache, 将用户信息保存在服务器缓存中)
绯村剑心 · 2006-10-09 · via 博客园 - 绯村剑心

由于某些原因,在我们的应用中会遇到一个用户只能在一个地方登录的情况,也就是我们通常所说的单点登录。在ASP.NET中实现单点登录其实很简单,下面就把主要的方法和全部代码进行分析。

实现思路

利用Cache的功能,我们把用户的登录信息保存在Cache中,并设置过期时间为Session失效的时间,因此,一旦Session失效,我们的Cache也过期;而Cache对所有的用户都可以访问,因此,用它保存用户信息比数据库来得方便。

查看示例

SingleLogin.aspx代码

<%@ Page language="c#" Codebehind="SingleLogin.aspx.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.aspx.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>用户名称:<asp:textbox id="UserName" runat="server"></asp:textbox></p> <p>用户密码:<asp:textbox id="PassWord" runat="server" TextMode="Password"></asp:textbox></p> <p><asp:button id="Login" runat="server" Text=" 登 录 "></asp:button></p> <p><asp:label id="Msg" runat="server"></asp: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.aspx.cc/Exam/SingleLogin.aspx } #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.aspx.cc/'>【孟宪会之精彩世界】"; Msg.Text += "</a>,祝您浏览愉快!:)</h4> "; } else { // 在 Cache 中发现该用户的记录,表名已经登录过,禁止再次登录 Msg.Text=" <h4 style='color:red'>抱歉,您好像已经登录了呀:-(</h4> "; return; } } } }