中国设计秀欢迎投稿
联网
平面设计 画册 VI欣赏 包装 CG-插画 搜索 个人网页 Alexa排名 CSS 建站资源 下载专区 JS特效 品牌服装 服装院校 专题欣赏 SEO 图标欣赏 专题
域名注册 虚拟主机 广州网站设计 域名注册 广州网站建设 上海网站建设 虚拟主机 广州网页设计 签名设计 虚拟主机 域名注册 acg王国 ACG玩家
求创科技
上海网麒科技
中国福网
数字引擎
招聘求职
中国互联
时代互联
中资源
当前位置:网络学院首页 >> 编程开发 >> .net >> ASP.NET 1.1 无 Cookie SessionID 重写

ASP.NET 1.1 无 Cookie SessionID 重写

来源:中国设计秀    作者:    点击:11     加入收藏    发表评论
0
顶一下
中国品牌设计网
浏览器的会话使用存储在 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.cnblogs.com/outman2008/archive/2007/02/25/655804.html
2007-03-09 17:13:00    出处:
热点文章/相关文章
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明 | 网站公告 | 友情链接 | 留言 | 旧版入口