在Angular 5 / ASP.NET Core 2项目上获取HTTP 400 Bad请求

时间:2018-02-13 20:56:32

标签: csrf asp.net-core-2.0 angular5

我在为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方法时,我得到了这个:

Request and response in Chrome

cookies

我收到HTTP 400错误请求错误...我做错了什么?

0 个答案:

没有答案