国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用Cache、Timer(ATLAS)控制用戶重復登陸的可行性方法

2019-11-18 16:45:10
字體:
來源:轉載
供稿:網友

       在我的前一篇文章《妙用Cache檢驗用戶是否重復登陸》,經過實踐和思考,發現忽略了一個很重要的地方:只是在登陸時,設置了一次登錄值到Cache中。如果Cache失效的時間設置久了,用戶一旦退出,在較短的時間間隔內重新登陸時,會發現無法登陸。但是如果失效時間設置短了,惡意登陸者又會在較短的時間內重新登陸,而且成功通過檢驗。顯然這種判斷方法是不完善的。

       我們需要怎么來改進這個時間的難題呢?設置一個較短的失效時間間隔,然后每隔一定時間,檢查一下Cache,把用戶登陸信息重新寫入Cache。那么只要用戶不退出網站系統,或者不關閉瀏覽器,這種判斷方法將會一直有效!那么,在WEB上,在asp.net下,什么東西能方便的實現計時器的效果呢?目前而言,最好的選擇無疑是 ATLAS 中的Timer控件!能夠設置計時器的啟動,間隔時間,以及間隔時間后做的事件。

 程序改進以后,分享如下,請參看程序注釋:
前臺頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "<html xmlns="<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Scr
iptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登陸" />
                    <br />
                    <br />
                    <asp:Label ID="Label1" runat="server" Width="350px"></asp:Label>
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="清除Cache" />&nbsp;
                    <asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="15000" OnTick="Timer1_Tick">
                    </asp:Timer>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

后臺程序

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    PRotected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //用戶名
            string sName = TextBox1.Text;

            //生成Key  
            string sKey = sName + "_Login";
           
            //得到Cache中的給定Key的值  
            string sUser = Convert.ToString(Cache[sKey]);

            //檢查是否存在  
            if (sUser == null || sUser == String.Empty)
            {
                session["username"] = sName;

                //Cache中沒有該Key的項目,表明用戶沒有登錄,或者已經登錄超時     
                //TimeSpan 表示一個時間間隔,獲取系統對session超時作的設置值
                //(如果考慮到允許用戶再次登陸的時間小于session超時時間,可將此值設小) 
                //TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
                //這里為了演示,把Cache保存時間間隔設置為了20秒
                TimeSpan SessTimeOut = new TimeSpan(0, 0, 0, 20, 0);
                HttpContext.Current.Cache.Insert(
                                                    sKey,
                                                    sKey,
                                                    null,
                                                    DateTime.MaxValue,
                                                    SessTimeOut,
                                                    System.Web.Caching.CacheItemPriority.NotRemovable,
                                                    null
                                                 );
               
                //啟動Timer
                this.Timer1.Enabled = true;

                //首次登錄,您可以做您想做的工作了。  
                Label1.Text = "你好!" + sName + "歡迎光臨";
            }
            else
            {
                //在Cache中發現該用戶的記錄,表示已經登錄過,禁止再次登錄  
                Label1.Text = "對不起,你的用戶身份已登陸";
                return;
            }
        }
        catch (System.Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //用戶名
        string sName = TextBox1.Text;

        //生成Key  
        string sKey = sName + "_Login";

        //為了測試方便,設置了這個從Cache中移出登陸信息的方法
        HttpContext.Current.Cache.Remove(sKey);

        Label1.Text = Session["username"] + " 的用戶登陸信息已從Cache清除!";
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            //用戶名
            string sName = TextBox1.Text;

            //生成Key  
            string sKey = sName + "_Login";

            //得到Cache中的給定Key的值  
            string sUser = Convert.ToString(Cache[sKey]);

            TimeSpan SessTimeOut = new TimeSpan(0, 0, 0, 20, 0);
            if (sUser != null)
            {
                HttpContext.Current.Cache.Remove(sKey);
            }
            HttpContext.Current.Cache.Insert(
                                                sKey,
                                                sKey,
                                                null,
                                                DateTime.MaxValue,
                                                SessTimeOut,
                                                System.Web.Caching.CacheItemPriority.NotRemovable,
                                                null
                                             );
        }
        else
        {
            this.Timer1.Enabled = false;
        }
    }
}
示例代碼:/Files/heekui/WebLogin.rar

后記:
1  這個方法對于判斷用戶重復登陸是可行的,但是同時伴隨著另一個問題點。設置了Timer,定時工作的話,只要不是正常退出,或者關閉瀏覽器的話,Session便永遠不會失效了。這樣作會有什么不好的效果嗎?
2  這個方法對每一個用戶而言都會定時向服務器發出請求,無疑會增加服務器端的負擔。若同時在線人數很多的情況下,這種請求是否會對服務器產生很大的影響。
    所以,只能說以上的這個方法只是一種可行的方法,但是否最優,沒有測試。不知各位還有什么更好的辦法沒有。
http://www.survivalescaperooms.com/heekui/archive/2007/01/08/615254.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 双鸭山市| 昔阳县| 黑河市| 乌鲁木齐县| 江城| 福鼎市| 卢龙县| 芮城县| 临桂县| 凉山| 桐梓县| 获嘉县| 德安县| 柘荣县| 蒙山县| 合水县| 郁南县| 黄平县| 汉沽区| 天镇县| 广德县| 惠来县| 张家港市| 新乡县| 吴江市| 南漳县| 襄汾县| 和田市| 万盛区| 广东省| 玉田县| 宁远县| 太仓市| 资中县| 时尚| 南皮县| 漯河市| 塔城市| 二连浩特市| 商河县| 枣庄市|