Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在集成测试中绕过表单post上的反伪造令牌验证_C#_Asp.net Core Mvc_Integration Testing_Antiforgerytoken - Fatal编程技术网

C# 如何在集成测试中绕过表单post上的反伪造令牌验证

C# 如何在集成测试中绕过表单post上的反伪造令牌验证,c#,asp.net-core-mvc,integration-testing,antiforgerytoken,C#,Asp.net Core Mvc,Integration Testing,Antiforgerytoken,我有一个ASP.NET core 2.2 MVC应用程序,它公开了一个带有基本表单(用户名/密码)的登录页面。控制器操作受AntiForgeryTokenAttribute保护,隐藏的\uu RequestVerificationToken由MVC添加 我正在使用TestServer编写集成测试,我想发送表单,看看是否得到302状态码,但找不到任何有效选项 我评估的一个选项是执行GET,提取_RequestVerificationToken,然后将tpoken作为表单的一部分提交。然而,这将不起

我有一个ASP.NET core 2.2 MVC应用程序,它公开了一个带有基本表单(用户名/密码)的登录页面。控制器操作受
AntiForgeryTokenAttribute
保护,隐藏的
\uu RequestVerificationToken
由MVC添加

我正在使用TestServer编写集成测试,我想发送表单,看看是否得到302状态码,但找不到任何有效选项

我评估的一个选项是执行GET,提取_RequestVerificationToken,然后将tpoken作为表单的一部分提交。然而,这将不起作用,因为我错过了饼干(我相信)
TestServer.CreateClient
不支持任何处理程序,因此无法添加Cookie

有没有办法测试这个


谢谢

所以需要做两件事:

  • 在页面获取期间:从标题中获取cookie并提取_请求验证
  • 提交表单时:在标题中添加cookie,并将u RequestVerification添加到模型中
  • 1.得到 您可以使用以下方法提取令牌:

    headers.FirstOrDefault(x => x.Key == "Set-Cookie").Value.First().Split(" ")[0];
    
    // The cookie we are looking for is .AspNetCore.Antiforgery.<unique guid>=<unique guid>
    var tokenCookie = cookieContent.Split("=");
    var name = tokenCookie[0];
    var value = tokenCookie[1];
    
    其中
    htmlContent
    HttpResponse.Content.ReadAsStringAsync()

    2.邮递 创建表单时,添加_RequestVerificationToken:

    new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
    {
        ... your stuff here
        new KeyValuePair<string, string>("__RequestVerificationToken", token)
            });
    }
    
    其中
    client
    是使用
    TestServer.CreateClient
    创建的
    HttpClient


    希望这对其他人有帮助

    所以需要做两件事:

  • 在页面获取期间:从标题中获取cookie并提取_请求验证
  • 提交表单时:在标题中添加cookie,并将u RequestVerification添加到模型中
  • 1.得到 您可以使用以下方法提取令牌:

    headers.FirstOrDefault(x => x.Key == "Set-Cookie").Value.First().Split(" ")[0];
    
    // The cookie we are looking for is .AspNetCore.Antiforgery.<unique guid>=<unique guid>
    var tokenCookie = cookieContent.Split("=");
    var name = tokenCookie[0];
    var value = tokenCookie[1];
    
    其中
    htmlContent
    HttpResponse.Content.ReadAsStringAsync()

    2.邮递 创建表单时,添加_RequestVerificationToken:

    new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
    {
        ... your stuff here
        new KeyValuePair<string, string>("__RequestVerificationToken", token)
            });
    }
    
    其中
    client
    是使用
    TestServer.CreateClient
    创建的
    HttpClient


    希望这对其他人有帮助

    为了更好地获得所需的标记和字段名,而不必从页面中删除HTML,我写了一篇博客文章和相同的示例代码,介绍如何使用应用程序部件来实现相同的目标:


  • 为了更好地获得所需的标记和字段名,而不必从页面中删除HTML,我写了一篇博客文章和相同的示例代码,介绍了如何使用应用程序部件来实现相同的目标: