gpt4 book ai didi

c# - 当前上下文中不存在名称 'AntiForgery'

转载 作者:行者123 更新时间:2023-12-05 03:08:33 41 4
gpt4 key购买 nike

我正在尝试在我的 Web Api 中实现 Antifrogery token ,但出现以下错误:

名称“AntiForgery”在当前上下文中不存在。

这是我的代码:

using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace MyAPI
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class ValidateAntiForgeryTokenAttribute: FilterAttribute
{

public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
try
{
string cookieToken = "";
string formToken = "";
//I am getting error on the line written below for "AntiForgery":
AntiForgery.Validate(cookieToken, formToken);
}
catch
{
actionContext.Response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.Forbidden,
RequestMessage = actionContext.ControllerContext.Request
};
return FromResult(actionContext.Response);
}
return continuation();
}

private Task<HttpResponseMessage> FromResult(HttpResponseMessage result)
{
var source = new TaskCompletionSource<HttpResponseMessage>();
source.SetResult(result);
return source.Task;
}
}
}

最佳答案

namespace System.Web.Helpers 存在于某些程序集中。

您必须将 System.Web.WebPages 程序集添加到您的项目中,然后像这样添加一个新的 using 语句:

解决方案资源管理器 -> [项目] -> 引用 ->(右键单击)添加引用... -> 扩展(选项卡)-> System.Web.WebPages -> 添加

using System.Web.Helpers;

关于c# - 当前上下文中不存在名称 'AntiForgery',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962828/

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