我遇到了这样的问题:cookie 永远不会被设置,因为我相信它使用了错误的cookie。这发生在
play.exposureevents.store
。我不知道怎么做,但是 exposureevents.com
cookie 可以在这个域上以某种方式使用。我相信在尝试进行身份验证时,它使用的是 .exposureevents.com cookie,并且从未设置正确的 cookie。 ASP.NET 中有没有一种方法可以删除错误的 cookie,或设置正确的 cookie?
登录时创建 Cookie
public SiteMemberModel CreateAuthenticationCookie(string username, Guid userId, string roles)
{
var member = GetMemberProfile(userId, username);
var ticket = new FormsAuthenticationTicket(
2,
username,
DateTime.Now,
DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
false,
JsonConvert.SerializeObject(member, Formatting.None),
FormsAuthentication.FormsCookiePath);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket))
{
Secure = true,
Path = FormsAuthentication.FormsCookiePath,
SameSite = SameSiteMode.None
};
if (!HttpContext.Current.Request.IsLocal)
{
cookie.Domain = Helper.GetDomain();
}
if (ticket.IsPersistent)
cookie.Expires = ticket.Expiration;
HttpContext.Current.Response.Cookies.Add(cookie);
return member;
}
Web.config
<httpCookies requireSSL="true" sameSite="None" />
<authentication mode="Forms">
<forms loginUrl="/login" path="/" cookieSameSite="None" requireSSL="true" protection="All" timeout="2880" slidingExpiration="true" name="_EXPOSURE_" />
</authentication>
浏览器 Cookie
任何设置 cookie 的后端组件通常都有一个外部 URL 和一个内部 URL。这里是 Kubernetes 设置的示例:
External URL: https:///www.example.com
Internal URL: http://applicationhost-svc.applications.svc:3000
计数的cookie是URL地址栏中对应浏览器的cookie,即外部URL。这就是用户代理在接收和发送 cookie 时将寻找的内容。
后端组件行为
因此,任何设置 cookie 并具有内部和外部 URL 的后端组件都必须具有诸如
COOKIE_DOMAIN
之类的配置设置,并应用它,以便 cookie 对用户代理正确工作。这包括以下任何一项:
在我工作的 Curity,我们的组件之一是 这个 .NET 实用程序 API,它可以向单页应用程序的浏览器发出 cookie。请注意,它配置了
COOKIE_DOMAIN
设置,并且 此代码 使用配置的 cookie 域。
第三方 Cookie
RFC6265bis规范于几年前发布,并引入了
SameSite
概念,其目标是保护用户隐私。随后所有主要浏览器都实现了这一点。
如果您为与浏览器地址栏中的 URL 不匹配的域设置 cookie,它将被归类为第三方 cookie 并被积极删除。也许这可以解释您的问题?
顺便说一下,上述规范还引入了更强大的 cookie,可以最好地防止跨站点请求伪造。这是
SameSite=strict
,应该由普通的 Web 应用程序使用。 SameSite=none
设置通常仅由授权服务器和身份提供商使用,用于管理外部化登录。
终于
如果我误解了您的问题,请回复。如果您解释了您的托管域名以及保护后端访问的表单身份验证 cookie 的名称,您的问题可能会得到改善。您的技术堆栈也可能正在设置一些辅助 cookie。还应该有一个设置来控制这些 cookie 的域。