All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP教程:用PHP程序对网页表单的处理_[PHP教程]
Delphi函数中参数的使用实例-方法
数据库的分页问题_[SQL Server教程]
PHP实例:PHP取GB2312编码字符串首字母的方法_php资料_编程技术
ASP.NET,2.0中预设的cookie_[Asp.Net教程]
让php5在win2003 X64 下运行的方法_[PHP教程]
详解网页制作中使用的HTML常用标记_[Html教程]
delphi创建指定的目录
如何用.NET技术在线生成网站LOGO_[Asp.Net教程]
使用自定义HTML标签来进行网页设计_[Html教程]
Reading number is top 10 pictures
A beautiful girl to bud2
西班牙山村小景5
无题
青涩甜美-王祖贤小时候的旧照片曝光
全球清廉国家排行
So beauty, will let you spray blood7
美丽的风景--让你目瞪口呆
Men don't mature ten sign
Hunan province aizhai super-large suspension bridge open to traffic and 4 world first1
Sora aoi in China3
Download software ranking
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
WebService在.NET中的实战应用教学视频 → 第3集
Boxer's Top ten classic battle8
Boxer vs Yellow2
The king of fighters 97(Mobile phone games-apk)
Eclipse 4.2.2 For Win32
Red cliff
Tram sex maniac 2 (H) rar bag14
Unix video tutorial10
徐若瑄成名作“魔鬼天使”
delv published in(发表于) 2014/1/16 9:27:33 Edit(编辑)
ASP.NET,1.1,无,Cookie,SessionID,重写_[Asp.Net教程]

ASP.NET,1.1,无,Cookie,SessionID,重写_[Asp.Net教程]

ASP.NET 1.1 无 Cookie SessionID 重写_[Asp.Net教程]

浏览器的会话使用存储在 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 () {}
}相关配置如下:







大家看了代码后就会知道,它实现的原理主要是因为不可能有相同IP电脑客户端同时访问一台服务器,当出现SessionID相同但他们客户端信息不同时就自动将后一个访问的客户端重定向以新建一个会话。

遗憾的是在WAP上应用时就有问题了,由于客户端信息没IP唯一标识(移动不给手机号信息了),所以如果相同型号的手机访问时就无法区分,不知哪位高人有没更好的解决办法,还望不吝赐教


题外话:工作忙,时间紧,抄得多,写得少,有问题,请留言。欢迎大家多交流沟通~~~


修正:汗一个先~~我忘了User-Agent 包括客户端的MAC地址或者手机的SM卡编号,所以除非用户故意欺骗 :(,我的方法还是有99%的成功率的 :) ,自己白操半天心。这个方法接近完美,呵呵。


来源:http://www.cnblogs.com/outman2008/archive/2007/02/25/655804.html







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.