Asp.net 子域共享cookie
2017-02-10 08:45
766 查看
最近项目遇到要共享cookie的问题,本来后台保存session用的是redis来保存数据的。所以只需要2个站点发的ASP.NET_SessionId是相同的就可以,并且它的Domain 是父级域名。实现code:
网上有类似参考地址:Asp.Net集群中Session共享
很多时候项目的cookie name 都是需要替换默认的ASP.NET_SessionId(需要读取配置),同时我们在开发的时候也不知道具体的父级域名(需要用正则表达式),所以具体实现code如下:
protected void Session_Start(object sender, EventArgs e)
{
SessionStateSection config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
string cookieName = config.CookieName;
Response.Cookies[cookieName].Value = Session.SessionID.ToString();
string url = Request.Url.ToString();
Regex reg = new Regex("([^.]*).([^.]*).com");
Match m = reg.Match(url);
if (m.Groups.Count > 2)
{
Response.Cookies[cookieName].Domain = $".{m.Groups[2].Value}.com";
}
}
protected void Session_Start(object sender, EventArgs e) { Response.Cookies["ASP.NET_SessionId"].Value = Session.SessionID.ToString(); Response.Cookies["ASP.NET_SessionId"].Domain = ".aa.com"; }
网上有类似参考地址:Asp.Net集群中Session共享
很多时候项目的cookie name 都是需要替换默认的ASP.NET_SessionId(需要读取配置),同时我们在开发的时候也不知道具体的父级域名(需要用正则表达式),所以具体实现code如下:
protected void Session_Start(object sender, EventArgs e)
{
SessionStateSection config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
string cookieName = config.CookieName;
Response.Cookies[cookieName].Value = Session.SessionID.ToString();
string url = Request.Url.ToString();
Regex reg = new Regex("([^.]*).([^.]*).com");
Match m = reg.Match(url);
if (m.Groups.Count > 2)
{
Response.Cookies[cookieName].Domain = $".{m.Groups[2].Value}.com";
}
}
相关文章推荐
- Asp.net 子域共享cookie
- ASP.NET中解决跨子域的Session共享
- 详解ASP.NET在不同的子域中共享Session(转)
- ASP.NET中在不同的子域中共享Session
- asp.net 实现一级域名与二级域名共享COOKIE
- asp与asp.net如何共享cookie
- ASP.NET中在不同的子域中共享Session的具体方法
- ASP.NET中在不同的子域中共享Session (转)
- ASP.NET中在不同的子域中共享Session(二级域名获取不到主域名下的session值)
- ASP.NET中在不同的子域中共享Session
- DataProtection设置问题引起不同ASP.NET Core站点无法共享用户验证Cookie
- ASP.NET中在不同的子域中共享Session
- .net 与 asp 共享 Cookie
- 关于Asp与Asp.NET共享Cookie需要注意的地方
- ASP.NET中在不同的子域中共享Session的具体方法
- ASP.NET中在不同的子域中共享Session
- asp.net 二级域名表单认证情况下共享Cookie
- ASP.NET中在不同的子域中共享Session
- ASP.NET中在不同的子域中共享Session
- 详解ASP.NET在不同的子域中共享Session