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();
}