自上次更新以来,我的Cookie出现错误,与SameSite属性相关。
Cookies来自第三方开发人员(Fontawesome,jQuery,Google Analytics,Google reCaptcha,Google Fonts等)。
Chrome控制台中的错误是这样的。
A cookie associated with a cross-site resource at <URL> was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at <URL> and <URL>.
(index):1 A cookie associated with a cross-site resource at http://jquery.com/ was set without the `SameSite` attribute. A future release of Chrome will …
Run Code Online (Sandbox Code Playgroud) 我正在使用带有旧后端的Angular 8(ASP.NET MVC 5框架),而不是核心。
我正在尝试发送网站的Cookie,因此来自角度网站的请求被认为已通过身份验证
我为此创建了一个拦截器
import { HttpInterceptor, HttpHandler, HttpEvent, HttpRequest }
from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const newRequest = request.clone({
withCredentials: true,
});
return next.handle(newRequest);
}
}
Run Code Online (Sandbox Code Playgroud)
这是请求的代码
private getDefaultConfiguration(): configurationsType {
return {
observe: 'response',
responseType: 'json',
headers: new HttpHeaders().set('Content-Type', 'application/json')
};
}
public async get(endpoint: string, params: HttpParams = undefined) …
Run Code Online (Sandbox Code Playgroud) 要在Chrome 80将有所变动SameSite准备,我从升级我的.NET框架API4.6.2
来4.7.2
。
我创建了一个简单的测试端点,它简单地设置了一个 cookie SameSite=None
:
public class TestController : ApiController
{
public IHttpActionResult Get()
{
var cookie = new HttpCookie("foo", "bar")
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.None
};
HttpContext.Current.Response.SetCookie(cookie);
return Ok();
}
}
Run Code Online (Sandbox Code Playgroud)
这在本地按预期工作,并返回以下标头:
set-cookie: foo=bar; path=/; secure; HttpOnly; SameSite=None
但是,这在发布到配置为 4.7 作为运行时堆栈的 Azure Web 应用程序时不起作用。Web 应用程序返回没有 SameSite 的 cookie 标头:
Set-Cookie: foo=bar; path=/; secure; HttpOnly
如果我将它设置为Strict
或者Lax
它也可以在 Azure 中按预期工作。
这是 Azure 的问题吗?是否需要在 Web 应用程序上配置任何内容才能使其正常工作,或者我可能必须以不同的方式设置 cookie?
为了避免 CSRF(跨站点请求伪造),大多数浏览器(自 2019 年末以来)自动考虑任何未明确定义 SameSite 属性的 cookie 将被视为 Lax,而不是之前的默认值 None。
最近(2020 年 2 月,自 Chrome 80 起)浏览器也忽略了定义 SameSite=None 且不安全的 cookie。
如何在我的 Web.config 中更改我的会话 cookie 以自动更改为无(以保持我的 SSO 集成工作)?
经过安全审核后,我要求将 cookie ASP.NET_sessionID 设置为“安全”。
现在标志没有设置。
我可以使用 SessionIDManager 将其设置为安全吗?使用以下代码登录后,我已经在使用它来更改会话 cookie 的值:
System.Web.SessionState.SessionIDManager manager = new System.Web.SessionState.SessionIDManager();
string oldId = manager.GetSessionID(System.Web.HttpContext.Current);
string newId = manager.CreateSessionID(System.Web.HttpContext.Current);
bool isAdd = false, isRedir = false;
manager.SaveSessionID(System.Web.HttpContext.Current, newId, out isRedir, out isAdd);
Run Code Online (Sandbox Code Playgroud)
编辑
我看到我可以设置
<httpCookies httpOnlyCookies="false" requireSSL="true" />
Run Code Online (Sandbox Code Playgroud)
但我只想让这一个 cookie 安全
cookies ×4
asp.net ×2
samesite ×2
angular ×1
asp.net-mvc ×1
azure ×1
c# ×1
csrf ×1
httpcookie ×1
https ×1
javascript ×1