我在为Angular 5 / ASP.NET Core 2项目配置XSRF时遇到了麻烦。
在我在Startup.ConfigureServices中添加的服务器上:
services.AddAntiforgery(options => { options.HeaderName = "X-XSRF-TOKEN"; });
并在Startup.Configure中:
app.Use(async (context, next) =>
{
var tokens = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN",
tokens.RequestToken, new CookieOptions
{
HttpOnly = false,
Secure = false
}
);
await next();
});
我将此方法称为测试:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Post([FromBody]string value)
{
return new JsonResult("good");
}
在Angular方面,我有app.module.ts:
imports: [
HttpClientModule,
HttpClientXsrfModule.withOptions({
cookieName: 'XSRF-COOKIE',
headerName: 'X-XSRF-TOKEN',
}), ...
然后,当我调用我的Post方法时,我得到了这个:
我收到HTTP 400错误请求错误...我做错了什么?