来自ASP.Net的带有XSRF-TOKEN的AngularJS返回400

时间:2018-02-04 01:05:11

标签: angularjs asp.net-core x-xsrf-token

我有一个非常奇怪的问题,试图在角度$ http的帖子调用中在ASP.Net Core中实现XSRF。

POST Register工作正常,并具有以下签名:

[HttpPost("Register")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> PostRegister([FromBody]AccountRegisterPostRequest req)

然而,POST Logout返回400并具有相同的签名:

[HttpPost("Logout")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> PostLogout()
{
   return Ok();
}

ConfigureServices:

services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
services.AddMvc();
services.Configure<MvcOptions>(c => c.Filters.Add(new RequireHttpsAttribute()));

配置:

app.Use(next => context =>
{
   if (string.Equals(context.Request.Path.Value, "/", StringComparison.OrdinalIgnoreCase))
   {
      var tokens = antiforgery.GetAndStoreTokens(context);
      context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken,
         new CookieOptions() { HttpOnly = false });
   }

   return next(context);
});

app.UseStaticFiles();

角度调用如下所示:

$http.post("/api/Account/Logout")
.then(function (result) {
   console.log(result);
})

API在同一解决方案中处于单独的项目中。 帮助:)

0 个答案:

没有答案