Search code examples
c#httpclientblazor-webassembly

How to set withCredentials=true in HttpClient


I want to set withCredentials=true in a C# HttpClient request - how to do this?

var res = await client.GetAsync("https://localhost:44387/api/values/SetCookie");

Solution

  • You can use the HttpClientHandler class and setting its UseDefaultCredentials property to true.

    var handler = new HttpClientHandler
    {
        UseDefaultCredentials = true,
        // Optionally, you can also set other properties of HttpClientHandler here
    };
    
    var client = new HttpClient(handler);
    var res = await client.GetAsync("https://localhost:44387/api/values/SetCookie");