2013-03-10 256 views
2

我试图配置DEV环境以支持在他们之间共享身份验证和会话的子域。表单身份验证和身份验证票据Cookie域

目前,我在DEV机器上配置了IIS和hosts文件来处理对mydomain,sd1.mydomain,sd2.mydomain,sd3.mydomain的请求。 Web应用程序本身按预期工作,我可以浏览所有子域上的所有页面,但需要验证的页面除外。当我尝试登录时,服务器端的所有内容都很完美(用户发现,创建cookie并添加到响应中),但cookie未到达浏览器(我试过Chrome和IE)。

我有一个创建并存储验证票据的代码,我在authentication.forms在web.config中设置域=“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上,而不在子域上。

我在做什么错了?

回答

2

卸下开始点,从domain=,你必须把它作为domain=".mydomain.com"与第一点作为这里所说http://www.w3.org/Protocols/rfc2109/rfc2109(第7页),感谢的@AlbatrossCafe

评论此设置上都cookie和身份验证。

+1

谢谢,它解决了我的问题。不知道cookie域值需要“.com/.something”。 – 2013-03-10 21:22:18

+0

@AlexDn欢迎您:) – Aristos 2013-03-10 21:22:41

+0

网络上的其他所有内容都指定您必须在域名前加点。 W3指定*明确指定的域必须始终以点开头* http://www.w3.org/Protocols/rfc2109/rfc2109 – AlbatrossCafe 2015-11-03 00:27:34

0

没有错。如果该域未在cookie上提供,则cookie应该仅用于发布域。

+0

我想设置域,但是当我这样做时,cookie不会到达浏览器。然后它不发送请求和FormAuthentication不起作用。我设置域.mydomain,并希望在mydomain和sd1.mydomain上使用它。 – 2013-03-10 21:13:59