0

I am struggling to send a Cookie from angular client to the server. I have used so far the code below:

let headers = new HttpHeaders();
headers.append('Cookie','Name=Janusz');

return this.http.post<any>(`${baseUrl}/refresh-token`, { 'headers': headers }, { withCredentials: true })

I checked it on the server side and that the cookie is not received by the server. Is that the correct way of sending the cookie from the client to the server in angular?

Additional comment:

I have found this code to set the cookie on the client side to be sent to the server, but I can't find a where are CookieContainer/HttpClientHandler/BaseAddress defined. What are those classes and which angular model I need to import to have access to them? Can someone please explain this to me? I need to get that to work.

var baseAddress = new Uri("http://example.com");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("foo", "bar"),
        new KeyValuePair<string, string>("baz", "bazinga"),
    });
    cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
    var result = await client.PostAsync("/test", content);
    result.EnsureSuccessStatusCode();
}
Janusz Dalecki
  • 197
  • 1
  • 10
  • Maybe helpful: https://stackoverflow.com/questions/17064791/http-doesnt-send-cookie-in-requests – rene Jul 22 '22 at 13:56
  • If I understood the trick is to use `{ withCredentials: true }` when using post command and whatever is set as cookie on the client side is sent to the server. Is that right? – Janusz Dalecki Jul 22 '22 at 14:11
  • Assuming you get this to work be careful what the server trusts from the client. Any user could manipulate that cookie value. – Crowcoder Jul 22 '22 at 14:13
  • That is my guess, yes. But I'm not an angular dev. The question does show debugging steps, at least that could be useful to verify that the browser isn't sending those cookies either. – rene Jul 22 '22 at 14:13

0 Answers0