I need to log in to the site, and continue to work with POST requests, but there is a problem on the site that is issued csrfmiddlewaretoken. Therefore, you must first go to the site, take the csrfmiddlewaretoken type "7e9001a3c0000f11099c11h119745e30" using the GET method and use it when logging in to the POST method, save the cookie after the login and work further. The problem is that the csrfmiddlewaretoken changes every time. Parameters that are sent at login

csrfmiddlewaretoken "7e9001a3c0000f11099c11h119745e30" username "логин" password "пароль" next "/about/" 

Been so

 WebResponse Response; HttpWebRequest Request; Uri url = new Uri("http://00.00.00.000:0000/accounts/login/"); CookieContainer cookieContainer = new CookieContainer(); Request = (HttpWebRequest)WebRequest.Create(url); Request.Method = "GET"; Request.CookieContainer = cookieContainer; Response = Request.GetResponse(); //1 раз получил csrfmiddlewaretoken string Parametros = "csrfmiddlewaretoken=" + cookieContainer.GetCookies(url)["csrftoken"].Value + "&username=логин&password=пароль&next=/about/"; Request = (HttpWebRequest)WebRequest.Create(url); //вот тут получается мне выдадут потом новый токен, а без использования этого я не знаю как сформировать новый запрос Request.Method = "POST"; Request.ContentType = "application/x-www-form-urlencoded"; Request.CookieContainer = cookieContainer; Request.Headers.Add("Cookie", Response.Headers.Get("Set-Cookie")); byte[] byteArray = Encoding.UTF8.GetBytes(Parametros); Request.ContentLength = byteArray.Length; Response = Request.GetResponse(); 

On the site it looks like this:

 <form method="POST" action="/accounts/login/" class="well" autocomplete="off"> <div style="display:none"><input type="hidden" name="csrfmiddlewaretoken" value="7e9001a3c2801f25097c11e119745e31" /></div> 

 Uri url = new Uri("http://00.00.00.000:0000/accounts/login/"); CookieContainer cookieContainer = new CookieContainer(); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "GET"; httpWebRequest.CookieContainer = cookieContainer; HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); string Parametros = "csrfmiddlewaretoken=" + cookieContainer.GetCookies(url)["csrftoken"].Value + "&username=логин&password=пароль&next=/about/"; Console.WriteLine(Parametros); //Paremetros "csrfmiddlewaretoken=d113dc9681a9ed800397f6164b608114&username=логин&password=пароль&next=/about/" httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = Parametros.Length; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; httpWebRequest.CookieContainer = cookieContainer; //httpWebRequest.Headers.Add("Cookie", httpWebRequest.Headers.Get("Set-Cookie")); using (Stream stream = httpWebRequest.GetRequestStream()) { byte[] paramAsBytes = Encoding.Default.GetBytes(Parametros); stream.Write(paramAsBytes, 0, paramAsBytes.Count()); } httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //тут получаю Удаленный сервер возвратил ошибку: (401) Несанкционированный 

What if

  //httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.Headers.Add("Cookie",httpWebRequest.Headers.Get("Set-Cookie")); 

Then I get "The remote server returned an error: (403) Forbidden."

  • You missed something. Perhaps you need to read about authorization and authentication in IIS. - nick_n_a
  • one
    I do not understand much about sisharpy, but is it possible to put together a cookieContainer and a cookie header in the request? - andreymal
  • I added this because I don’t understand if 1st Request.CookieContainer = cookieContainer; Well, even if I delete this line, nothing changes. - 220VOLT

1 answer 1

Removed from & next = / about /

 string Parametros = "csrfmiddlewaretoken=" + cookieContainer.GetCookies(url)["csrftoken"].Value + "&username=логин&password=пароль&next=/about/"; string Parametros = "csrfmiddlewaretoken=" + cookieContainer.GetCookies(url)["csrftoken"].Value + "&username=логин&password=пароль"; 

And it worked, although in the browser it is transmitted enter image description here