在 .net Core 中,我們使用 IAntiforgery 配置防偽功能以及 [ValidateAntiForgeryToken] 或 AutoValidateAntiforgeryToken 來防止跨站點請求偽造 (XSRF/CSRF) 攻擊。
在我們使用的中間件中配置防偽功能
var antiforgery = app.Services.GetRequiredService<IAntiforgery>();
app.Use((context, next) =>
{
var requestPath = context.Request.Path.Value;
if (string.Equals(requestPath, "/", StringComparison.OrdinalIgnoreCase)
|| string.Equals(requestPath, "/index.html", StringComparison.OrdinalIgnoreCase))
{
var tokenSet = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokenSet.RequestToken!,
new CookieOptions { HttpOnly = false });
}
return next(context);
});
微軟檔案鏈接
現在我的問題是如果我們設定new CookieOptions { HttpOnly = True });那么我們需要在服務器端和客戶端做哪些更改
uj5u.com熱心網友回復:
客戶端的變化?實際上,絕對沒有。
使用 HTTPOnly cookie 應該更容易,而不是手動提取和存盤客戶端 cookie/token。HttpOnly cookie 只是阻止 cookie 被客戶端 JavaScript 攔截。只要您實際上并未嘗試從請求中獲取該 cookie(為什么要,它存盤在 cookie 中!),那么它將自動與您的所有請求一起發送。
服務器端應該像往常一樣作業。HttpOnly 是客戶端的變化
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/384709.html
標籤:C# 。网 asp.net核心 csrf x-xsrf-令牌
