Cookies 有人知道如何使用u RequestVerificationToken向应用程序发送API调用吗?(错误:cookie和表单字段已交换)
我正在尝试使用VS2019(C#)和RestSharp通过API调用测试防伪令牌。我正在做的是进入我们的登录页面,获取4个令牌/cookie,然后将它们附加到一篇文章中,使用用户名和密码,尝试登录。第一个调用成功并返回HTTP 200和4个COOKIE/令牌(ASP.NET_SessionId、XSRF-TOKEN、XSRF-COOKIE和a__RequestVerificationToken-所有这些都作为COOKIE(在cookiecontainer中)附加到POST API调用),但是第二个调用失败,HTTP 500显示以下消息:“验证提供的防伪令牌失败。cookie“\uuu RequestVerificationToken”和表单字段“\uuu RequestVerificationToken”已交换。“。我在我的POST调用中包含此令牌两次-一次作为cookie,一次作为请求正文的一部分。这是我的代码-有人能解释如何修复此错误吗 谢谢 杰米Cookies 有人知道如何使用u RequestVerificationToken向应用程序发送API调用吗?(错误:cookie和表单字段已交换),cookies,token,restsharp,antiforgerytoken,x-xsrf-token,Cookies,Token,Restsharp,Antiforgerytoken,X Xsrf Token,我正在尝试使用VS2019(C#)和RestSharp通过API调用测试防伪令牌。我正在做的是进入我们的登录页面,获取4个令牌/cookie,然后将它们附加到一篇文章中,使用用户名和密码,尝试登录。第一个调用成功并返回HTTP 200和4个COOKIE/令牌(ASP.NET_SessionId、XSRF-TOKEN、XSRF-COOKIE和a__RequestVerificationToken-所有这些都作为COOKIE(在cookiecontainer中)附加到POST API调用),但是第二
public void LogIn(string userName, string password)
{
// 1st call to get the cookies and tokens.
CommonProperties.Client = new RestClient { CookieContainer = new CookieContainer() };
CommonProperties.Client.BaseUrl = new Uri($"https://localhost:50000/Account/Login");
var request = new RestRequest(Method.GET);
request.AddParameter("ReturnUrl", "%2F", ParameterType.QueryString);
CommonProperties.Response = CommonProperties.Client.Execute(request);
CommonProperties.Client.BaseUrl = new Uri($"https://localhost:50000/Account/Login");
var requestToken = CommonProperties.Response.Cookies.Single(c => c.Name ==
"__RequestVerificationToken");
// 2nd call to log in.
request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept-Encoding", "gzip, deflate, br");
request.AddHeader("Accept", "*/*");
request.AddHeader("Referer", $"https://localhost:50000/Account/Login");
request.AddParameter("undefined", $"__RequestVerificationToken=
{requestToken.Value}&UserName=userName&Password=password_321", ParameterType.RequestBody);
CommonProperties.Response = CommonProperties.Client.Execute(request);
}