.NET - Añadir ASPNET Cookies a una HTTP Request (HttpClient)

Oct 22, 2014 9:00:00 AM / by daniel.comas

 

 

Un problema que muchos os podéis encontrar en las peticiones HTTP desde HttpClient del Framework 4.5 de .NET, es el de que no se pasan las cookies de sessión!

 

Vamos a conocer el comportamiento normal y como podemos añadirlas facilmente!

 

 

Comportamiento normal

 

  • Request

 

Dim httpClient = New HttpClient()
httpClient.BaseAddress = New Uri("http://localhost:53640")
httpClient.DefaultRequestHeaders.Accept.Add(New Headers.MediaTypeWithQualityHeaderValue("application/json"))

Dim result = httpClient.GetAsync("/api/users?id=daniel.comas").Result
Dim resultContent As String = result.Content.ReadAsStringAsync().Result

 

  • La petición será ésta
GET /api/users?id=daniel.comas HTTP/1.1
Connection: Keep-Alive
Accept: application/json
Host: localhost:53640

 

 

Vamos a hacer la misma petición pero ahora añadiendo las cookies de sessión

 

  • Método para añadir las Aspnet Cookies

 

Private Shared Sub AddAspnetCookies(ByRef cookieContainer As CookieContainer)
For Each cookieName As String In HttpContext.Current.Request.Cookies
Dim authCookie As HttpCookie = HttpContext.Current.Request.Cookies(cookieName)

Dim oC As New Cookie()
oC.Domain = HttpContext.Current.Request.Url.Host
oC.Expires = authCookie.Expires
oC.Name = authCookie.Name
oC.Path = authCookie.Path
oC.Secure = authCookie.Secure
oC.Value = authCookie.Value
cookieContainer.Add(oC)
Next
End Sub

 

  • Request 

 

Dim cookieContainer = New CookieContainer()
Dim handler = New HttpClientHandler()
handler.UseCookies = True
handler.CookieContainer = cookieContainer
Dim httpClient = New HttpClient(handler)
httpClient.BaseAddress = New Uri("http://localhost:53640")
httpClient.DefaultRequestHeaders.Accept.Add(New Headers.MediaTypeWithQualityHeaderValue("application/json"))

AddAspnetCookies(cookieContainer)

Dim result = httpClient.GetAsync("/api/users?id=daniel.comas").Result
Dim resultContent As String = result.Content.ReadAsStringAsync().Result

 

  • Ahora La petición será ésta

 

GET /api/users?id=daniel.comas HTTP/1.1
Accept: application/json
Cookie: quck-note-current=%7B%22Id%22%3A%22%22%2C%22Note%22%3A%22%22%7D;
ASP.NET_SessionId=qqll5at0bstjjdgglnoymgqr;
.AUXPROASSIST=94BD2B74EAC3CFF7A56C57391F9614997B6B8A1375607F3CC8E1AAA25EE512BF64CC2C3599E30D1B69D118A3A4B8D9E8013DF66E1469D289372BD625F5BAB9E4C42DE3C172D6A9630D1F65CB54E09A5D6E2B80CA9A284A79ED1BF01AF92351F23B07584426B63ED6D037EBD0D71A58435C09DA9E5E420D51BD63E56E0468529DAE18D6D2;
.ASPXROLES=a4WQ4HIPrcl9vgUMPAabdo-yb6qTCNDSoRvmLizHUC0iB7-TmOHaV6C8HALCcZb6JxFw18pjkbK3wqYDMJIHEcfLASpZb75bpe-xQmuA-ss_Te7yQFwlUqZKdt-8_kHrZAuj03FCXKAGrmbRobm0zhMXubcl9X604EaasSnwSdzYOiilEodjO7PQPVkMepgqFqkTiEgt4Rts1zfLLj6MLKhBLhIN5AHFbMEdF97G-_BRnVBkD9V3xOPxtppxH9wbYQXVyBDS2YXShoBY_sSAe-apMhki0KYxX7F95udoNWBfchMv2JL2jgA37CUBegf8r5vVmGR0hAZ7NDXoYveNfnd1S9Zr7CpD2zHdujJNUZtTjvXQj9yx5FrzhV77PYtaj4PQkd7CuLuDj5MR32j6nBmZEw6-eGSFiLy3PEa1PdmNloNLglKDsCJKdzw4jBycnkiIo50L6_uqNMjm1bXyDGgq4599kwhIIsYKykXmHuSroM-yabXqP-cFtLNoTnVamRbiRYBO7wJiYArGyB2jXfEAaQMpcbXtbEtDKsw4O054iwF80
Host: localhost:53640

 

 

 

 

Topics: ASP, Cookies, HTTP, .NET, Programación, Sessión

daniel.comas

Written by daniel.comas

Lists by Topic

see all
Servicios gestionados

Categorías

Ver todas