0

I'm write here because I was trying to send a POST request to a server api,
I tried to send also another request(the first one) and from the response it work({"success":"true", "role":"USER"}). But in the sencond request as a response I get: {"timestamp":1524589409895,"status":403,"error":"Forbidden","message":"Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.","path":"/api/v1/rec"}.
So I put all the cookies but one of them "XSRF -TOKEN" cause my program to crashSystem.Net.CookieException: The 'Name'='XSRF -TOKEN' part of the cookie is invalid
so I discovered that this cookie change every time you create a session
so I tried to get the cookies from the response of the first message and added on the header for the second one, and this is the result

I also set the NETFramework at the 4.5 version
I'm leaving a temporary account here for you so you can try this without creating an account only for a test
Request Payload from firefox

static void Main(string[] args)
{
    Uri uri = new Uri("https://www.vcast.it/api/v1/rec");
    cookieContainer = new CookieContainer();
    cookieContainer.Add(uri, new Cookie("CONSENT", "true"));
    cookieContainer.Add(uri, new Cookie("_ga", "GA1.2.940742918.1524584758"));
    cookieContainer.Add(uri, new Cookie("_gid", "GA1.2.1691132054.1524584758"));
    cookieContainer.Add(uri, new Cookie("remember-me", "Z1hvUnJoOHdIM3dCZ2pmYXVKamFRUT09OkpxSXUzRDVRUXd6UG14eGlVUlJMOXc9PQ"));

    clienthandler = new HttpClientHandler { AllowAutoRedirect = true, UseCookies = true, CookieContainer = cookieContainer };
    client = new HttpClient(clienthandler);
    client.DefaultRequestHeaders.Host = "www.vcast.it";

    MainAsync();
}
private static CookieContainer cookieContainer;
private static HttpClientHandler clienthandler;
private static HttpClient client;

static async void MainAsync()
{
    Uri uri = new Uri("https://www.vcast.it");
    var values = new Dictionary<string, string>
    {
       { "username", "XXXX" },
       { "password", "XXXX" },
       { "remember-me", "undefined" },
       { "submit", "" }
    };
    var content = new FormUrlEncodedContent(values);
    HttpResponseMessage response = await client.PostAsync("https://www.vcast.it/apiLogin?appId=58aef0c4ea5d52b2c0e4f2ed", content);
    Console.WriteLine(await response.Content.ReadAsStringAsync());

    Console.WriteLine("New Cookies:");
    var responseCookies = cookieContainer.GetCookies(uri).Cast<Cookie>();
    foreach (var cook in responseCookies)
    {
        cookieContainer.Add(uri, cook);
        Console.WriteLine(cook.Name + ":" + cook.Value);
    }

    Console.WriteLine();
    clienthandler = new HttpClientHandler { UseCookies = true, CookieContainer = cookieContainer };
    client = new HttpClient(clienthandler);

    values = JsonConvert.DeserializeObject<Dictionary<string, string>>("{\"name\":\"Titolo registrazione\",\"fromSuggestion\":false,\"manual\":true,\"followSeries\":false,\"resolution\":\"r576\",\"format\":\"MP4\",\"defaultProvider\":\"vcloud\",\"provider\":\"vcloud\",\"channelId\":\"58138235c9e77c00018242ed\",\"startDate\":1524585300000,\"endDate\":1524588900000,\"startHour\":17,\"startMinute\":55,\"endHour\":18,\"endMinute\":55}");
    content = new FormUrlEncodedContent(values);
    client.DefaultRequestHeaders.Referrer = new Uri("https://www.vcast.it/manualRec/");
    response = await client.PostAsync("https://www.vcast.it/api/v1/rec", content);

    Console.WriteLine(await response.Content.ReadAsStringAsync());
}

I will gladly accept any kind of comment or answer

Ryan C
  • 572
  • 5
  • 18
  • 1
    don't provide username and password! – Daniel May 07 '18 at 18:51
  • "XSRF -TOKEN" has an extra space between F and dash. – jdweng May 07 '18 at 19:02
  • I said that i create that temporary account for the testing so if you want to try and run the code you don't have to create a new account, it isn't my personal account – Luca Sandri May 07 '18 at 20:58
  • thank you jdweng for reminding me, I also tried that but nothig really changed – Luca Sandri May 07 '18 at 21:07
  • @LucaSandri I am facing the exact same issue. Did you eventually solve this issue? If yes, can you please post the solution? – Sohi Sep 14 '18 at 22:28
  • yes, sorry for the late reply @Sohi, I solved the problem by circumventing it, in fact I used the selenium library to create a session as if it were controlled by a user, I leave you a link to start (https://www.youtube.com/watch?v=_GkWt0k4eG0) – Luca Sandri Sep 17 '18 at 07:21

0 Answers0