gpt4 book ai didi

asp.net - AntiForgery Token 使用 ASP.NET5 Web API,在 NET46 上没有 System.Web.Helpers

转载 作者:行者123 更新时间:2023-12-05 01:00:14 31 4
gpt4 key购买 nike

尝试在 ASP.NET5(又名 vNext)API 上实现 AntiForgery

我找到的所有文章都来自 this article并使用 System.Web.Helpers.AntiForgery.GetTokens 这不应该是 ASP.NET5 的方式

private static string GetTokenHeaderValue() 
{
string cookieToken, formToken;
System.Web.Helpers.AntiForgery.GetTokens(null, out cookieToken, out formToken);
return cookieToken + ":" + formToken;
}

是否有任何实现实际上显示了如何在 ASP.NET5 中检索这些 token

附加来源 ASP.NET5 AntiForgery Source Code

最佳答案

在 Controller 生成

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;
}
}
}

在 View 中生成
@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));

关于asp.net - AntiForgery Token 使用 ASP.NET5 Web API,在 NET46 上没有 System.Web.Helpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29680098/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com