日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

實現(xiàn)單點登錄(附全碼)_AX - AXzhz - 博客園

 xnet 2007-01-17

實現(xiàn)單點登錄(附全碼)_AX

①概念:
單點登錄就是同一時刻某一用戶只能在一個地點登錄系統(tǒng).
②實現(xiàn)
通過Cache來保證用戶只能登錄一次.因為Cache是Application level的.
不過猜想當(dāng)用戶同時在線量很大時會出現(xiàn)問題,10萬的用戶Cache可不是說著玩的,不過一般除了網(wǎng)游好像很難達到這個數(shù)量級.
③本文源自【孟憲會之精彩世界】,略做修改,特此感謝!

附:源碼
前臺:

 1<%@ Page language="c#" Codebehind="SingleLogin.aspx.cs" AutoEventWireup="false"
 2    Inherits="eMeng.Exam.SingleLogin" 
%>
 3<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 4<HTML>
 5    <HEAD>
 6        <title>單點登錄測試</title>
 7        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 8        <meta http-equiv="Author" content="孟子E章">
 9        <meta http-equiv="WebSite" content="http://dotnet.">
10        <style>H3 {
11    FONT: 17px 宋體
12}

13INPUT {
14    FONT: 12px 宋體
15}

16SPAN {
17    FONT: 12px 宋體
18}

19{
20    FONT: 12px 宋體
21}

22H4 {
23    FONT: 12px 宋體
24}

25
</style>
26    </HEAD>
27    <body ms_positioning="GridLayout">
28        <form id="Form1" method="post" runat="server">
29            <div align="center">
30                <h3>單點登錄測試</h3>
31                <p>用戶名稱:<asp:TextBox id="UserName" runat="server"></asp:TextBox></p>
32                <p>用戶密碼:<asp:TextBox id="PassWord" runat="server" TextMode="Password"></asp:TextBox></p>
33                <p><asp:Button id="Login" runat="server" Text=" 登  錄 "></asp:Button></p>
34                <p><asp:Label id="Msg" runat="server"></asp:Label></p>
35            </div>
36        </form>
37    </body>
38</HTML>
39

后臺:

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Web;
  7using System.Web.SessionState;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.HtmlControls;
 11
 12namespace eMeng.Exam
 13{
 14    /// <summary>
 15    /// SingleLogin 的摘要說明。
 16    /// 實現(xiàn)單點登錄
 17    /// </summary>

 18    public class SingleLogin : System.Web.UI.Page
 19    {
 20        protected System.Web.UI.WebControls.TextBox UserName;
 21        protected System.Web.UI.WebControls.TextBox PassWord;
 22        protected System.Web.UI.WebControls.Label Msg;
 23        protected System.Web.UI.WebControls.Button Login;
 24
 25        private void Page_Load(object sender, System.EventArgs e)
 26        {
 27            //字符串倒置
 28            string s="hello_XA";
 29            string ss="";
 30            for(int i=s.Length-1;i>=0;i--)
 31            {
 32                ss+=s[i];
 33            }

 34            Response.Write(ss);
 35            Response.Write("<br>"+DateTime.MaxValue+"||"+System.Web.HttpContext.Current.Session.Timeout);
 36            // 實際例子可訪問:
 37            // http://dotnet./Exam/SingleLogin.aspx
 38        }

 39
 40        Web 窗體設(shè)計器生成的代碼
 58
 59        private void Login_Click(object sender, System.EventArgs e)
 60        {
 61//            //Session不能實現(xiàn)單點登錄
 62//            string key=UserName.Text + "_" + PassWord.Text;
 63//            string k=Convert.ToString(Session["user"]);
 64//            if(k==""||k==null)
 65//            {
 66//                Session["user"]=key;
 67//                Response.Write("首次登錄!");
 68//            }
 69//            else
 70//            {
 71//                Response.Write("已經(jīng)登錄!");
 72//            }
 73
 74
 75            // 作為唯一標識的Key,應(yīng)該是唯一的,這可根據(jù)需要自己設(shè)定規(guī)則。
 76            // 做為測試,這里用用戶名和密碼的組合來做標識;也不進行其它的錯誤檢查。
 77
 78            // 生成Key
 79            string sKey = UserName.Text + "_" + PassWord.Text;
 80            // 得到Cache中的給定Key的值
 81            string sUser = Convert.ToString(Cache[sKey]);
 82            // 檢查是否存在
 83            if (sUser == null || sUser == String.Empty)
 84            {
 85                // Cache中沒有該Key的項目,表名用戶沒有登錄,或者已經(jīng)登錄超時
 86                // 注意下面使用的TimeSpan構(gòu)造函數(shù)重載版本的方法,是進行是否登錄判斷的關(guān)鍵。???好象不是哦!
 87                //SessTimeOut是設(shè)定的一個TimeSpan,該處設(shè)定為Session登錄超時的時間,我本機為20分鐘,
 88                //見Page_Load的最后一行輸出
 89                TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
 90                HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut,
 91                    System.Web.Caching.CacheItemPriority.NotRemovable,null);
 92                // 首次登錄,您可以做您想做的工作了。
 93                Msg.Text="<h4 style=‘color:red‘>嗨!歡迎您訪問<a href=‘http://dotnet./‘>【孟憲會之精彩世界】";
 94                Msg.Text+="</a>,祝您瀏覽愉快?。海?lt;/h4>";
 95                Msg.Text+="<h4 style=‘color:red‘>AX也祝您愉快!</h4>";
 96            }

 97            else
 98            {
 99                // 在 Cache 中發(fā)現(xiàn)該用戶的記錄,表名已經(jīng)登錄過,禁止再次登錄
100                Msg.Text="<h4 style=‘color:red‘>抱歉,您好像已經(jīng)登錄了呀:-(</h4>";
101            }

102        }

103    }

104}

105

posted on 2007-01-11 11:03 斧頭幫少幫主 閱讀(161) 評論(5)  編輯 收藏 引用 網(wǎng)摘 所屬分類: 經(jīng)驗總結(jié)

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多