跨站点共享Session解决方案、单点登录解决方案(.NET 2.0版本)
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 />");
}
}
此例子只是提供了一种解决思路,在应用过程中应根据项目不同进行调整及详细设计。