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

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

ASP.NET 1.1 無 Cookie SessionID 重寫

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

         瀏覽器的會話使用存儲在 sessionID 屬性中的唯一標識符進行標識。會話 ID 使 asp.net 應用程序能夠將特定的瀏覽器與 Web 服務器上相關的會話數據和信息相關聯。會話 ID 的值在瀏覽器和 Web 服務器間通過 Cookie 進行傳輸,如果指定了無 Cookie 會話,則通過 URL 進行傳輸。
         ASP.NET 通過自動在頁的 URL 中插入唯一的會話 ID 來保持無 Cookie 會話狀態。例如,下面的 URL 已被 ASP.NET 修改,以包含唯一的會話 ID lit3py55t21z5v55vlm25s55:
               http://www.example.com/s(lit3py55t21z5v55vlm25s55)/orderform.aspx
         如果一個包含無 Cookie SessionID 的鏈接被多個瀏覽器共享時(可能通過搜索引擎或其他程序),此行為可能導致對會話數據的意外共享。可以通過禁用會話標識符的回收來降低多個客戶端共享會話數據的可能性。為此,將 sessionState 配置元素的 regenerateExpiredSessionId 屬性設置為 true。這樣,在使用已過期的會話 ID 發起無 Cookie 會話請求時,將生成一個新的會話 ID。
                                                                                                                               ——摘自 MSDN

         .NET2.0中我們已可以通過重寫SessionIDManager 類來改變SessionID 的生成機制和驗證方法來防止會話數據的意外共享(即出現多個瀏覽器被識別為同一個會話,共用一個Session),可在.NET1.1中卻沒有相關的類讓我們改變SessionID 的生成機制(封裝死了),但受一篇文章的啟發,我們可以在SessionID 生成之后對它進行處理。文章是老外寫的,由于本人閱讀能力有限,偶可沒時間去看一大版唧唧歪歪的鷹文,直接下了代碼來看。還好代碼不算多,思路也很清晰,大概了解了他實現重寫SessionID的原理,我改了下在無Cookie 會話中完美實現了。
相關代碼
using System;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Configuration;
using System.Security.Cryptography;
using System.Runtime.Serialization;
using System.Globalization;
using System.Text;

public class SecureSessionModule : IHttpModule
{
    PRivate static string _ValidationKey = null;

    public void Init (Httpapplication app)
    {
        if (_ValidationKey == null)
            _ValidationKey = GetValidationKey ();
        app.AcquireRequestState+=new EventHandler(app_AcquireRequestState);
    }

    void app_AcquireRequestState (Object sender, EventArgs e)
    {
        _ValidationKey=GetValidationKey();//每天生成一個KEY提高安全性

        HttpContext current  = ((HttpApplication) sender).Context;

        //將處理后的SessionID存在Session["ASP.NET_SessionID"]中
        string sessionid = GetSession (current, "ASP.NET_SessionID");

        if (sessionid != null)
        {
            if (sessionid.Length <= 24)
                RedirectUrl(current);

            string id = sessionid.Substring (0, 24);
            string mac1 = sessionid.Substring (24);

            string mac2 = GetSessionIDMac (id, current.Request.UserHostAddress, current.Request.UserAgent, _ValidationKey);

            // 用戶客戶端信息發生的變化,比對失敗
            if (String.CompareOrdinal(mac1, mac2) != 0)
            {
                RedirectUrl(current);
            }
        }
        else
        {
            RedirectUrl(current);
        }
    }

    private void RedirectUrl(HttpContext current)
    {
         //重定向頁面以重新生成新的SessionID
         current.Response.Redirect(current.Request.Url.ToString(),true);
    }

    private string GetValidationKey ()
    {
        string key = DateTime.Now.ToShortDateString();

        return key;
    }

    private string GetSession (HttpContext current, string name)
    {
        object id = FindSession(current.Session,name);
        if (id == null)
        {
            // 將用戶客戶端信息加密存儲在Session中以便比對
            id= current.Session.SessionID+GetSessionIDMac (current.Session.SessionID, current.Request.UserHostAddress,
                current.Request.UserAgent, _ValidationKey);
            current.Session[name]  = id;
        }
        return id.ToString();
    }

    private object FindSession (HttpSessionState session, string name)
    {
        return session[name];
    }

    private string GetSessionIDMac (string id, string ip, string agent, string key)
    {
        StringBuilder builder = new StringBuilder (id, 512);       
        builder.Append (ip);
        builder.Append (agent);

        using (HMACSHA1 hmac = new HMACSHA1 (Encoding.UTF8.GetBytes (key)))
        {
            return Convert.ToBase64String (hmac.ComputeHash (
                Encoding.UTF8.GetBytes (builder.ToString ())));
        }
    }

     public void Dispose () {}
}相關配置如下:
<configuration>
  <system.web>
    <httpModules>
      <add name="SecureSession" type="SecureSessionModule,SecureSessionModule" />
    </httpModules>
  </system.web>
</configuration>

       大家看了代碼后就會知道,它實現的原理主要是因為不可能有相同IP電腦客戶端同時訪問一臺服務器,當出現SessionID相同但他們客戶端信息不同時就自動將后一個訪問的客戶端重定向以新建一個會話。
     
       遺憾的是在WAP上應用時就有問題了,由于客戶端信息沒IP唯一標識(移動不給手機號信息了),所以如果相同型號的手機訪問時就無法區分,不知哪位高人有沒更好的解決辦法,還望不吝賜教

題外話:工作忙,時間緊,抄得多,寫得少,有問題,請留言。歡迎大家多交流溝通~~~
http://www.survivalescaperooms.com/outman2008/archive/2007/02/25/655804.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜兰县| 廉江市| 盖州市| 荔波县| 乳源| 双鸭山市| 巍山| 石楼县| 岳阳市| 清涧县| 株洲县| 城口县| 眉山市| 老河口市| 民勤县| 锦州市| 洪湖市| 汶上县| 克拉玛依市| 丽水市| 山西省| 金平| 洞头县| 鄯善县| 河源市| 昌吉市| 曲沃县| 桃江县| 莱西市| 红原县| 晋州市| 永安市| 方正县| 临江市| 淳安县| 宁明县| 河北省| 金华市| 石城县| 曲阜市| 通海县|