zoukankan      html  css  js  c++  java
  • (转)在ASP.NET 中实现单点登录(利用Cache, 将用户信息保存在服务器缓存中)

    实现思路

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

    SingleLogin.aspx

    View Code
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SingleLogin.aspx.cs" Inherits="LuckyWeb.SingleLogin" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>单点登录测试</title>
        <style type="text/css">
            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.cs

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace LuckyWeb
    {
        public partial 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;
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            #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
    
            protected void Login_Click(object sender, 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;
    
                }
            }
        }
    }

    效果:

  • 相关阅读:
    CentOS查看CPU信息、位数、多核信息
    Linux常用命令大全
    chmod命令详细用法
    tar命令的详细解释
    yum和rpm命令详解
    LeetCode 241. Different Ways to Add Parentheses
    LeetCode 139. Word Break
    LeetCode 201. Bitwise AND of Numbers Range
    LeetCode 486. Predict the Winner
    LeetCode 17. Letter Combinations of a Phone Number
  • 原文地址:https://www.cnblogs.com/lucky_hu/p/2893520.html
Copyright © 2011-2022 走看看