gpt4 book ai didi

asp.net - FormsAuthentication.SetAuthCookie 未设置路径或域?

转载 作者:行者123 更新时间:2023-12-02 21:07:44 28 4
gpt4 key购买 nike

我有一个网络应用程序可以安装在很多域和路径上。

所以:

  • client1Name.{mySite.com}
  • client2Name.{mySite.com}
  • 演示。{mySite.com}/prospect1Name
  • 演示。{mySite.com}/prospect2Name
  • 演示。{mySite.com}/prospect3Name

相同代码的所有单独应用程序实例。

问题是,如果客户端登录 client1Name.{mySite.com},然后访问其他站点之一,他们的浏览器将发送身份验证 cookie。

在所有情况下,FormsAuthentication.SetAuthCookie 都不会设置PathDomain

我期望的是:

  • client1Name.{mySite.com} - = client1Name.{mySite.com} 路径 = /
  • client2Name.{mySite.com} - = client2Name.{mySite.com} 路径 = /
  • demo.{mySite.com}/prospect1Name - = demo.{mySite.com} 路径 = /prospect1Name
  • demo.{mySite.com}/prospect2Name - = demo.{mySite.com} 路径 = /prospect2Name
  • demo.{mySite.com}/prospect3Name - = demo.{mySite.com} 路径 = /prospect3Name

我可以手动覆盖 .Net 的行为来显式设置这些,但我不确定为什么我需要这样做 - 确保这应该是设置身份验证 cookie 时的默认行为,或者至少是一个无需重新设置即可设置的选项-写大块内容。

我错过了什么吗?有没有办法让 FormsAuthentication.SetAuthCookie 设置 PathDomain

如果不是,动态读取最佳PathDomain的最佳方法是什么?相同的代码必须在所有站点上运行,我不想添加进一步的配置 key 。

更新

这是我当前的解决方案:

// replacement for FormsAuthentication.SetAuthCookie(user.UserName, false);
// as that fails to limit the cookie by domain & path and fails.

var cookie = FormsAuthentication.GetAuthCookie(username, false);
cookie.HttpOnly = true;
cookie.Path = this.Request.ApplicationPath;
cookie.Secure = string.Equals("https", this.Request.Url.Scheme, StringComparison.OrdinalIgnoreCase);

// the browser will ignore the cookie if there are fewer than two dots
// see cookie spec - http://curl.haxx.se/rfc/cookie_spec.html
if (this.Request.Url.Host.Split('.').Length > 2)
{
// by default the domain will be the host, so www.site.com will get site.com
// this may be a problem if we have clientA.site.com and clientB.site.com
// the following line will force the full domain name
cookie.Domain = this.Request.Url.Host;
}

this.Response.Cookies.Add(cookie);

但是,对于 FormsAuthentication.SetAuthCookie 应该能够做到的事情来说,这似乎有很多解决方法。这真的是最好的方法吗?

最佳答案

我必须进行大量挖掘,但看起来 FormsAuthentication.SetAuthCookie 不支持此功能的原因是因为它不应该 - IIS不应该永远在身份验证cookie上设置路径,原因如下......

Cookie 路径区分大小写,因此:

  • http://站点/路径
  • http://site/PATH

浏览器有 2 个不同的 cookie - 它们(IE、FX、Safari、Opera 或 Chrome)都不会将 /PATH 的 cookie 发送到 /path 或反之亦然。

IIS 不区分大小写,但始终将 URL 重置为 ASP 应用程序名称的大小写。

这意味着,如果 IIS 应用程序名为“PATH”并且用户转到 http://site/path,那么他们将被重定向到 http://site/path 登录/site/PATH/LogOn?ReturnUrl=/path 通过 IIS/ASP.Net

成功登录后,用户将被重定向回指定的 ReturnUrl,因此:

  1. 用户转到http://site/path
  2. 由 IIS 发送至 http://site/PATH/LogOn?ReturnUrl=/path
  3. 输入登录详细信息并提交
  4. 响应将 cookie 设置为 /PATH 并将位置设置为 /path(由 ReturnUrl 定义)
  5. 重定向回http://site/path
  6. 浏览器无法识别/path,它只有一个/PATH的cookie,因此什么也不发送!
  7. 没有向应用程序发送 cookie,因此它会重定向回 http://site/PATH/LogOn?ReturnUrl=/path
  8. 转到第 2 步并重复。

如果用户使用 http://site/path 作为他们永远无法登录的应用程序的 URL,这会给用户带来问题。

除此之外,如果他们已经登录到 http://site/PATH 并收到了 URL,例如向 http://site/path/发送电子邮件resources/id,他们将被要求重新登录,并且无法到达新路径。

这意味着除非您需要 /PATH/path 是完全不同的站点(不太可能在某些仅限 UNIX 的环境之外),否则您永远不应该在身份验证时设置路径属性 cookies 。

关于asp.net - FormsAuthentication.SetAuthCookie 未设置路径或域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695736/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com