当前位置: 首页 > news >正文

共享Session、单点登录解决方案(ASP.NET2.0)

出处:http://pzling.blog.hexun.com/25546546_d.html

利用序列化机制实现Session共享的原理:

1、Web Server 1的应用程序序列化Session信息为文本值(可以是Binary或Soap格式)

2、将序列化后的值写入文件,保存到File Server上

3、Web Server 2 对保存在File Server上的Session序列化后的值进行反序列化

4、在Web Server 2上重新构建Session值


下面我们来详细看看实现代码,分以下几个步骤:


1、创建一个类库工程:ShareSession

引入以下的命名空间:

System.configuration
System.Runtime.Serialization.Formatters.Soap
System.Web

2、创建一个类:SessionEntity

代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ShareSession
...{
/**//* *****************************
*
*Author:Xiaojun Liu
*Create Date :2007.11.09
*Description :
*
* *****************************
*/



[Serializable]
public class SessionEntity
...{
[NonSerialized]
public string AUTH_GUID = "";

private Hashtable _HtShareSession;

public Hashtable HtShareSession
...{
get
...{
return this._HtShareSession;
}
set
...{
this._HtShareSession = value;
}
}


public SessionEntity(string guid)
...{
this.AUTH_GUID = guid;
}

}//


}//



3、创建一个序列化、反序列化操作的类:ShareSessionFormatter

代码如下:

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.IO;

namespace ShareSession
...{

/**//* *****************************
*
*Author:Xiaojun Liu
*Create Date :2007.11.09
*Description :
*
* *****************************
*/


/** <summary>
/// 格式化操作器
/// </summary>
public class ShareSessionFormatter
...{

private static string _ShareSessionPath = System.Configuration.ConfigurationManager.AppSettings["ShareSessionPath"].ToString();

/** <summary>
/// 序列化格式器类型
/// </summary>
public enum FormatterType
...{
Binary,
Soap
};

/** <summary>
/// 执行序列化
/// </summary>
/// <param name="formatterType">类型</param>
public static void Serialize(FormatterType formatterType)
...{
if (HttpContext.Current.Session["AUTH_GUID"] == null)
...{
HttpContext.Current.Session["AUTH_GUID"] = Guid.NewGuid().ToString();
}
Hashtable ht = new Hashtable();

//遍历Session
foreach (string key in HttpContext.Current.Session.Contents.Keys)
...{
ht.Add(key, HttpContext.Current.Session[key]);
}

/**/执行序列化
switch (formatterType)
...{
case FormatterType.Binary:
BinarySerialize(ht);
break;
case FormatterType.Soap:
SoapSerialize(ht);
break;
default:
break;
}

}

/** <summary>
/// 按照Binary格式序列化
/// </summary>
/// <param name="ht"></param>
private static void BinarySerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;

// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();

//执行序列化
formatter.Serialize(stream, obj);
stream.Close();

// 将对象置空
obj = null;
}

/** <summary>
/// 按照Soap格式序列化
/// </summary>
/// <param name="ht"></param>
private static void SoapSerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;

// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();

//执行序列化
formatter.Serialize(stream, obj);
stream.Close();

// 将对象置空
obj = null;
}


/** <summary>
/// 反序列化
/// </summary>
/// <param name="formatterType">传送端的Session["AUTH_GUID"]</param>
/// <param name="guid"></param>
public static void Deserialize(FormatterType formatterType, string guid)
...{
switch (formatterType)
...{
case FormatterType.Binary:
BinaryDeserialize(guid);
break;
case FormatterType.Soap:
SoapDeserialize(guid);
break;
default:
break;
}
}


/** <summary>
/// 以Binary方式反序列化
/// </summary>
private static void BinaryDeserialize(string guid)
...{
SessionEntity obj = new SessionEntity(guid);

// 打开文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();

obj = (SessionEntity)formatter.Deserialize(stream);
stream.Close();

Hashtable ht = obj.HtShareSession;
obj = null;

//遍历ht,生成Session
CreateSession(ht);
}

/** <summary>
/// 以Soap方式反序列化
/// </summary>
private static void SoapDeserialize(string guid)
...{
SessionEntity obj = new SessionEntity(guid);

// 打开文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Open);
SoapFormatter formatter = new SoapFormatter();

obj = (SessionEntity)formatter.Deserialize(stream);
stream.Close();

Hashtable ht = obj.HtShareSession;
obj = null;

//遍历ht,生成Session
CreateSession(ht);
}

/** <summary>
/// 遍历Hashtable,同时创建Session
/// </summary>
/// <param name="ht"></param>
private static void CreateSession(Hashtable ht)
...{
foreach (DictionaryEntry de in ht)
...{
HttpContext.Current.Session[de.Key.ToString()] = de.Value;
}
}


}//
}//



4、编译项目,生成ShareSession.dll

5、把ShareSession.dll引入Web Server 1上的应用程序中,同时在Web.config文件中增加配置字节

代码如下:

<!-- Session信息序列化后保存路径 -->
<add key="ShareSessionPath" value="C:\ShareSession\"/>


6、在Web Server 1上的应用程序中新建一个页面,功能是初始化Session,然后把Session信息序列化,代码如下:


前台代码:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Session共享</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lHttp_Cookie" runat="server" Text=""></asp:Label>
<a href=" http://localhost:5000/Front/Test/ShareSession.aspx?AUTH_GUID=<%=AUTH_GUID %>">Go Other Web Site(port:5000)</a>
</div>
</form>
</body>
</html>

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Front_Test_ShareSession : System.Web.UI.Page
...{
public string AUTH_GUID = "";

protected void Page_Load(object sender, EventArgs e)
...{
Session.Clear();
Session.Abandon();
Session.Add("USER_ID", "2002");
Session.Add("USER_NAME", "Xiaojun Liu");

ShareSession.ShareSessionFormatter.Serialize(ShareSession.ShareSessionFormatter.FormatterType.Soap);
AUTH_GUID = Session["AUTH_GUID"].ToString();


}
}



7、在Web Server 2上进行第5步操作

8、在Web Server 2上的应用程序中新建一个页面,功能是反序列化,还原Session,同时读取Session信息进行测试,代码如下:

前台代码:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Session共享</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lHttp_Cookie" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Front_Test_ShareSession : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
string guid = Request.Params["AUTH_GUID"].ToString();
ShareSession.ShareSessionFormatter.Deserialize(ShareSession.ShareSessionFormatter.FormatterType.Soap,guid);

Response.Write("USER_ID = "+Session["USER_ID"].ToString()+"<br />");
Response.Write("USER_NAME = "+Session["USER_NAME"].ToString()+"<br />");
Response.Write("AUTH_GUID = " + Session["AUTH_GUID"].ToString() + "<br />");
}
}



此例子只是提供了一种解决思路,在应用过程中应根据项目不同进行调整及详细设计。

相关文章:

  • Net开源项目:SSO单点登录方案,Net开源工作流
  • VBScript调用RAR压缩文件...
  • 天津渤海早报防甲流“中药处方”,供大家参考
  • 表单开发 工作流平台开发的主要任务是开发表单,要在工作流平台上展现(工作流系统开发体系 )...
  • B/S结构工作流设计器(VML),工作流程定义
  • (转贴)用VML开发工作流设计器 UCML.NET工作流管理系统
  • 试用期程序员应该了解的事儿
  • 试用期程序员应该了解的事儿——《程序员羊皮卷》连载(15)
  • 我是如何带领团队开发工作流项目的
  • SVN 版本管理
  • Win32 OpenGL系列专题
  • Nebula3 SDK Nov 2009 更新内容
  • iPhone 多线程编程
  • 面试中如何自我介绍如何回答我的优缺点
  • [常见问题]iPhone NSURLConnection delegate methods得不到调用
  • [deviceone开发]-do_Webview的基本示例
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • Druid 在有赞的实践
  • Java|序列化异常StreamCorruptedException的解决方法
  • python 装饰器(一)
  • Python_网络编程
  • rabbitmq延迟消息示例
  • SpiderData 2019年2月13日 DApp数据排行榜
  • STAR法则
  • supervisor 永不挂掉的进程 安装以及使用
  • TypeScript实现数据结构(一)栈,队列,链表
  • windows下如何用phpstorm同步测试服务器
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 分布式任务队列Celery
  • 计算机常识 - 收藏集 - 掘金
  • 记一次和乔布斯合作最难忘的经历
  • 京东美团研发面经
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 前端js -- this指向总结。
  • 入手阿里云新服务器的部署NODE
  • 山寨一个 Promise
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • # 计算机视觉入门
  • #QT(TCP网络编程-服务端)
  • (LeetCode 49)Anagrams
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (二)fiber的基本认识
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (未解决)macOS matplotlib 中文是方框
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .libPaths()设置包加载目录
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • ??javascript里的变量问题
  • @Autowired注解的实现原理
  • @Bean, @Component, @Configuration简析