【发布时间】:2017-05-20 06:20:45
【问题描述】:
我想在 ASP.NET Core 应用程序中验证 AntiForgery 令牌。我知道我可以按照 SO post here 中的建议在 Action 方法上添加 [AutoValidateAntiforgeryToken] 或 [ValidateAntiforgeryToken] 属性来单独做到这一点
我正在寻找全局方法来验证所有 POST 方法的令牌。所以我创建了一个中间件来做到这一点。但是我找不到合适的方法来验证令牌。就像在经典的 asp.net 中一样,有 AntiForgery.Validate()。
ASP.NET Core 中的等效方法是什么
public class ValidateAntiForgeryTokenMiddleware
{
private readonly RequestDelegate _next;
public ValidateAntiForgeryTokenMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Method.ToUpper() == "POST")
{
// where does this mehod exists?
// i could not find it in Microsoft.AspNetCore.Antiforgery namespace
AntiForgery.Validate();
}
await _next(httpContext);
}
}
public static class ValidateAntiForgeryTokenMiddlewareExtensions
{
public static IApplicationBuilder UseValidateAntiForgeryToken(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ValidateAntiForgeryTokenMiddleware>();
}
}
【问题讨论】:
标签: asp.net-core asp.net-core-mvc .net-core coreclr