我正在尝试将 DEV 环境配置为支持在子域之间共享身份验证和会话的子域。
目前,我在 DEV 机器上配置了 IIS 和 hosts 文件来处理对 mydomain、sd1.mydomain、sd2.mydomain、sd3.mydomain 的请求。Web 应用程序本身按预期工作,我可以浏览所有子域上的所有页面,但需要身份验证的页面除外。当我尝试登录时,服务器端的一切看起来都很完美(用户找到了,cookie 创建并添加到响应中),但是 cookie 没有到达浏览器(我尝试了 Chrome 和 IE)。
我有一个创建和存储身份验证票证的代码,我在 web.config 的 authentication.forms 中设置了 domain=".mydomain" :
var now = DateTime.UtcNow.ToLocalTime();
var ticket = new FormsAuthenticationTicket(
1 /*version*/, _user.Username, now, now.Add(FormsAuthentication.Timeout),
isPersistentCookie, _user.Username, FormsAuthentication.FormsCookiePath);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
cookie.Secure = FormsAuthentication.RequireSSL;
cookie.Path = FormsAuthentication.FormsCookiePath;
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
}
_httpContext.Response.Cookies.Add(cookie);
当我调试时,上面的代码工作正常,用户是正确的,并且具有正确域的 cookie 被添加到响应中。
如果我从 web.config 中删除 domain=".mydomain",则身份验证有效,但仅适用于 mydomain 而不是子域。
我做错了什么?