您的位置:首页 > 编程语言 > ASP

Asp.Net StateServer实现共同域名下Session共享

2017-03-09 12:16 513 查看
概述

在实验的时候,参照了多方的信息,确实成功了,这里简单记录一下。

我们知道,在Asp.Net的Web.Config文件中,System.Web节点下,有个sessionState节点,它说明了应用程序的Session处理方式,它有如下几个选项:

public class ShareSessionModule : IHttpModule
{

#region IHttpModule 成¨¦员

void IHttpModule.Dispose()
{
//throw new Exception("The method or operation is not implemented.");
}

void IHttpModule.Init(HttpApplication context)
{
//throw new Exception("The method or operation is not implemented.");
Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType("System.Web.SessionState.OutOfProcSessionStateStore");
FieldInfo uriField = stateServerSessionProvider.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);

if (uriField == null)
throw new ArgumentException("UriField was not found");

uriField.SetValue(null, ".test.com");
context.EndRequest += new EventHandler(this.EndRequest);

}

private void EndRequest(object sender, EventArgs args)
{
HttpApplication application = sender as HttpApplication;

for (int i = 0; i < application.Response.Cookies.Count; i++)
{

application.Response.Cookies[i].Domain = ".test.com";
}
}

#endregion
}


View Code

第三步:将自定义HttpModule加入Web.config

<httpModules>
<add name="CookieTest" type="WebApplication1. ShareSessionModule,WebApplication1"/>
</httpModules>


参考文档
http://sai5d.blog.163.com/blog/static/62225483201010211393132/ https://msdn.microsoft.com/zh-cn/library/h6bb9cz9(VS.80).aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: