Angular2 ASP.NET Core AntiForgeryToken

Dan*_*bdn 14 xss web-services asp.net-core angular

我有一个Angular2应用程序.它在ASP.NET 5(Core)中运行.
它使Http调用控制器工作正常.

但现在我需要建立Cross Site Scripting投影.

如何在每个Http请求上生成新令牌,然后在Angular2应用程序中执行AntiForgeryToken检查?

注意:我在Angular中的数据表单不是从MVC视图生成的,而是完全用Angular2编写的,只调用Web服务.

我见过的所有例子都已过时,不起作用/不起作用.

如何整合在AntiForgeryToken检查Angular2对ASP.NET 5,其中形式是纯粹的角?

谢谢.

Dan*_*anO 6

不需要自定义操作筛选器.它可以在Startup.cs中连接起来.

using Microsoft.AspNetCore.Antiforgery;

(...)

public void ConfigureServices(IServiceCollection services)
{
  services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");

  (...)
}

public void Configure(IApplicationBuilder app, IAntiforgery antiforgery)
{
  app.Use(next => context =>
  {
    if (context.Request.Path == "/")
    {
      //send the request token as a JavaScript-readable cookie, and Angular will use it by default
      var tokens = antiforgery.GetAndStoreTokens(context);
      context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions { HttpOnly = false });
    }
    return next(context);
  });

  (...)
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要在控制器中的所有内容是[ValidateAntiForgeryToken]装饰器,无论您希望强制执行哪个令牌.

作为参考,我在这里找到了这个解决方案 - AspNet AntiForgery Github第29期.


小智 5

我正在使用操作过滤器来发送请求令牌。只需将其应用于您想要新防伪令牌的操作,例如 Angular2 SPA、WebAPI 操作等。

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class AngularAntiForgeryTokenAttribute : ActionFilterAttribute
{
    private const string CookieName = "XSRF-TOKEN";
    private readonly IAntiforgery antiforgery;

    public AngularAntiForgeryTokenAttribute(IAntiforgery antiforgery)
    {
        this.antiforgery = antiforgery;
    }

    public override void OnResultExecuting(ResultExecutingContext context)
    {
        base.OnResultExecuting(context);

        if (!context.Cancel)
        {
            var tokens = antiforgery.GetAndStoreTokens(context.HttpContext);

            context.HttpContext.Response.Cookies.Append(
                CookieName,
                tokens.RequestToken,
                new CookieOptions { HttpOnly = false });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
/* HomeController */

[ServiceFilter(typeof(AngularAntiForgeryTokenAttribute), IsReusable = true)]
public IActionResult Index()
{
    return View();
}

/* AccountController */

[HttpPost()]
[AllowAnonymous]
[ValidateAntiForgeryToken]
// Send new antiforgery token
[ServiceFilter(typeof(AngularAntiForgeryTokenAttribute), IsReusable = true)]
public async Task<IActionResult> Register([FromBody] RegisterViewModel model)
{
    //...
    return Json(new { }); 
}
Run Code Online (Sandbox Code Playgroud)

在 Startup 中注册该属性,并配置 Antiforgery 服务读取请求令牌形式的“X-XSRF-TOKEN”头。

public class Startup
{
    // ...

    public void ConfigureServices(IServiceCollection services)
    {
        // ...

        services.AddScoped<AngularAntiForgeryTokenAttribute>();
        services.AddAntiforgery(options =>
        {
            options.HeaderName = "X-XSRF-TOKEN";
        });
    }
}
Run Code Online (Sandbox Code Playgroud)