尝试在AntiForgery (又名vNext) API上实现vNext
我发现的所有文章都来自这篇文章,并且使用了这篇文章,这不应该是ASP.NET5的方式
private static string GetTokenHeaderValue()
{
string cookieToken, formToken;
System.Web.Helpers.AntiForgery.GetTokens(null, out cookieToken, out formToken);
return cookieToken + ":" + formToken;
}是否有任何实现实际演示如何在ASP.NET5中检索这些令牌?
发布于 2015-04-18 05:34:10
在控制器处生成
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.DependencyInjection;
namespace MyApp.App.Controllers
{
public class MyController : Controller
{
public string GetAntiForgeryTokens()
{
var antiForgery = Context.RequestServices.GetService<AntiForgery>();
AntiForgeryTokenSet antiForgeryTokenSet = antiForgery.GetTokens(Context, null);
string output = antiForgeryTokenSet.CookieToken + ":" + antiForgeryTokenSet.FormToken;
return output;
}
}
}在视图中生成
@inject AntiForgery antiForgery
@functions
{
public string GetAntiForgeryTokens()
{
AntiForgeryTokenSet antiForgeryTokenSet = antiForgery.GetTokens(Context, null);
string output = antiForgeryTokenSet.CookieToken + ":" + antiForgeryTokenSet.FormToken;
return output;
}
}
<body>
@GetAntiXsrfToken()
</body>验证
var antiForgery = Context.RequestServices.GetService<AntiForgery>();
antiForgery.Validate(Context, new AntiForgeryTokenSet(formToken, cookieToken));发布于 2016-06-17 21:58:05
现在有一个源代码中的示例。样品使用角度为1.x。在该示例的基础上,通过使用过滤器进行验证,下面是一个粗略的示例。在Startup,需要设置令牌:
public void Configure(IApplicationBuilder app, IAntiforgery antiforgery)
{
app.Use(next => context =>
{
if (!context.Request.Path.Value.StartsWith("/api/"))
{
// We can 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);
});
…
}然后,可以使用一个过滤器来验证令牌:
public class AntiforgeryValidationFilter : IAsyncActionFilter
{
private readonly IAntiforgery antiforgery;
public AntiforgeryValidationFilter(IAntiforgery antiforgery)
{
this.antiforgery = antiforgery;
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (context.HttpContext.Request.Path.Value.StartsWith("/api/"))
{
await antiforgery.ValidateRequestAsync(context.HttpContext);
}
await next();
}
}然后在Startup中注册过滤器
public void ConfigureServices(IServiceCollection services)
{
// Angular's default header name for sending the XSRF token.
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
services
.AddMvc(options =>
options.Filters.Add(typeof(AntiforgeryValidationFilter)))
…
}发布于 2015-12-30 18:24:16
使用MVC实现此功能的另一种方法是使用HTML助手获取视图上的令牌,这将同时创建cookie和隐藏的输入字段令牌。
<form>
@Html.AntiForgeryToken()
</form>HTML输出:
<input name="__RequestVerificationToken" type="hidden" value="*** />现在表单令牌已经存在,我们可以在报头中发送令牌,或者将它添加到API所期望的模型中。API可以使用处理自定义防伪令牌验证的ActionFilter进行保护。类似于授权的工作方式
[ValidateAntiForgeryToken]
public class MyController : ApiController逻辑是读取可以在其中找到cookie和标头值标记的标头。然后,可以通过调用
AntiForgery.Validate(cookieToken, headerToken);然后,过滤器可以接受或拒绝请求。
通过返回拒绝:
HttpStatusCode.Forbidden
https://stackoverflow.com/questions/29680098
复制相似问题