我的POST请求在C#中失败,但在POSTMAN中可以正常工作

时间:2019-08-22 13:28:48

标签: c# post postman csrf

我向服务器本地安装的nextcloud安装发出登录请求。 我传递了3个参数:requesttoken,密码,shareToken。该请求在邮递员中运行正常,但在我的C#代码中失败。

我收到以下回复 带有412状态代码。

  

“ {\”消息\“:\” CSRF检查失败\“}”

我复制粘贴了从邮递员请求中获得的c#代码,如下所示。有任何线索吗?

    var client = new RestClient("https://nextcloud.xxxx.org/index.php/s/Sryt58drbe34/authenticate/showShare");
    var request = new RestRequest(Method.POST);
    request.AddHeader("Cache-Control", "no-cache");
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("undefined", "requesttoken=oq%2Fqo4Nx0eNvYyty5QNeJqPbjZxMCtn3bFvUfEi%2Fwcg%3D%3AlOWrxPECsptWMBwGnTYMQ%2FG3yPENe7iQQwz7EQOLmYA%3D&password=bb9AYoqWSmAxiCgvFusW&sharingToken=Sryt58drbe34", ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);

1 个答案:

答案 0 :(得分:0)

您的帖子应该是这样的

var uri = "https://nextcloud.xxxx.org/index.php/s/Sryt58drbe34/authenticate/showShare"
var requestToken= "oq/qo4Nx0eNvYyty5QNeJqPbjZxMCtn3bFvUfEi/wcg=:lOWrxPECsptWMBwGnTYMQ/G3yPENe7iQQwz7EQOLmYA=";
var password = "bb9AYoqWSmAxiCgvFusW";
var shareToken = "Sryt58drbe34";

var formContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("requesttoken", requestToken), 
    new KeyValuePair<string, string>("password", password ),
    new KeyValuePair<string, string>("sharetoken ", shareToken )
});

var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(uri.ToString(), formContent);