asp.net网站不同子域名共享session信息
Posted xuexiaodong2009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net网站不同子域名共享session信息相关的知识,希望对你有一定的参考价值。
1session信息可序列化 [Serializable]
[Serializable]
public class UserSession
public string GroupCode
get;
set;
public string RoleCode
get;
set;
2 使cookie的path为根域名而不是子域名
function setCookie(name, value) //两个参数,一个是cookie的名子,一个是值
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString()+"; path=/";
3开启服务aspnet_state.exe
4修改web.config的sessionState
<!--<sessionState mode="InProc" cookieless="false" timeout="600" />-->
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" timeout="60"></sessionState>
5修改Global.asax文件
在 void Session_Start(object sender, EventArgs e)中添加调用如下代码
private void InitSession()
foreach (string moduleName in this.Modules)
string appName = "APPNAME";
IHttpModule module = this.Modules[moduleName];
SessionStateModule ssm = module as SessionStateModule;
if (ssm != null)
System.Reflection.FieldInfo storeInfo = typeof(SessionStateModule).GetField("_store", System.Reflection.BindingFlags.Instance | BindingFlags.NonPublic);
SessionStateStoreProviderBase store = (SessionStateStoreProviderBase)storeInfo.GetValue(ssm);
if (store == null)//In IIS7 Integrated mode, module.Init() is called later
FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.Static | BindingFlags.NonPublic);
HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance | BindingFlags.NonPublic);
appNameInfo.SetValue(theRuntime, appName);
else
Type storeType = store.GetType();
if (storeType.Name.Equals("OutOfProcSessionStateStore"))
FieldInfo uribaseInfo = storeType.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
uribaseInfo.SetValue(storeType, appName);
if (Response.Cookies != null)
for (int i = 0; i < Response.Cookies.Count; i++)
if (Response.Cookies[i].Name == "ASP.NET_SessionId")
Response.Cookies[i].Domain = ".dhcc.com.cn"; //一定要保持不同应用程序的"ASP.NET_SessionId"的cookie的Domain值一致
void Session_Start(object sender, EventArgs e)
InitSession();
以上是关于asp.net网站不同子域名共享session信息的主要内容,如果未能解决你的问题,请参考以下文章
Thinkphp框架下(同服务器下)不同二级域名之间session互通共享设置
ASP.NET下跨应用共享Session和使用Redis进行Session托管