angular - Angular 9 未在请求中设置 X-XSRF-TOKEN 标头
问题描述
我必须保护我的 Web 应用程序免受 CSRF 的影响,CSRF 是一个在客户端使用 Angular 9 的 .Net 核心 MVC Web 应用程序。
这是我尝试过的
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
services.AddMvc(option => option.EnableEndpointRouting = false);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAntiforgery antiforgery)
{
app.Use((context, next) =>
{
// CSRF cookie token generation
string path = context.Request.Path.Value;
if (
string.Equals(path, "/", StringComparison.OrdinalIgnoreCase) ||
string.Equals(path, "/index.html", StringComparison.OrdinalIgnoreCase))
{
// The request token can be sent as a JavaScript-readable cookie,
// and Angular uses it by default.
var tokens = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken,
new CookieOptions() { HttpOnly = false });
}
// CSRF cookie token generation - end
return next.Invoke();
});
}
它正在生成 XSRF-TOKEN cookie,但 Angular 没有在请求中设置 X-XSRF-TOKEN 标头。 我没有在 Angular 请求部分进行任何代码更改。
Controller.cs
[ValidateAntiForgeryToken]
[HttpPost]
public IActionResult ProduceMessage([FromBody] OncRequestData oncRequestData)
{
OncRequestData _OncRequestData = new OncRequestData();
}
在 Angular 应用程序中,我添加了一个 httpintercepter 来提取并发送请求标头中的令牌
export class TokenInterceptorService implements HttpInterceptor {
token: string;
constructor(private xsrfTokenExtractor: HttpXsrfTokenExtractor) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(req.method == "POST")
{
let xsrfToken = this.xsrfTokenExtractor.getToken();
const authReq = req.clone({ headers: req.headers.set("X-XSRF-TOKEN", xsrfToken) });
return next.handle(authReq);
}else{
return next.handle(req);
}
}
现在我从服务器收到一个错误的请求错误(400)
解决方案
我通过进行以下更改暂时解决了这个问题。
- 我从操作方法中删除了 [ValidateAntiForgeryToken] 属性
- 添加了一个自定义中间件来验证所有 POST 请求
- 触发 ValidateRequestAsync(httpContext); 验证请求的方法。
public class AntiforgeryValidationMiddleware
{
private readonly RequestDelegate _next;
public AntiforgeryValidationMiddleware(RequestDelegate next)
{
_next = next;
}
/// <summary>
/// Validate incoming request for CSRF token
/// </summary>
/// <param name="httpContext"></param>
/// <param name="antiforgery"></param>
/// <returns></returns>
public async Task InvokeAsync(HttpContext httpContext, IAntiforgery antiforgery)
{
try
{
// if the POST request is not from AD2BC call back
if (httpContext.Request.Method == "POST" && !httpContext.Request.Path.Equals("/Home/Auth"))
{
await antiforgery.ValidateRequestAsync(httpContext);
}
await _next(httpContext);
}
catch (AntiforgeryValidationException exception)
{
Log.Error("CSRF token validation failed" + exception.Message);
}
}
}
如果有人有更好的解决方案,请告诉我
推荐阅读
- sql - 选择独特的列组合
- java - 如何访问父类型的 ArrayList 中的子方法?
- javascript - Firefox 不尊重 Greasemonkey 脚本中的 Object.defineProperty()?
- python - django 如何在不设置字段值的情况下创建模型值?
- json - 如何在单个字段上使用 jq 过滤 JSON,但显示多个字段?
- java - 我在批处理作业期间面临空指针
- ios - 如何在 swift 中在 nib 类之间使用协议委托?
- java - Java 8 多级分组和归约
- regex - 如何使用 PDI 组件过滤来自 postgres 的选择性数据?
- node.js - 使用 Node-Jasper 包在 Node.js 中生成报告