Possible to allow setting of UseCookies property in JsonHttpClient?

I’m attempting to get a ServiceStack client to work with Blazor WASM and it seems the closest I got is with JsonHttpClient since it is based on the newer Microsoft HttpClient which is what Blazor WASM uses. Unfortunately, it seems that it doesn’t like the use of cookies

Property UseCookies is not supported

Is it possible to make the following line configurable as a property of the JsonHttpClient? I’m hoping that setting it to False will allow it to run.

I figured out a way to do this

((HttpClientHandler)client.HttpMessageHandler).UseCookies = false;

But for some reason “client” is null which is strange because according to the source code above, it should be initialized internally. Maybe it’s just not supported.

I’ve added a top-level UseCookies in the latest v5.8.1 so you should be to disable it with:

var client = new JsonHttpClient { UseCookies = false };

But it’s not clear what HttpClient features are supported in Blazor, which will most likely need to be configured with a custom HttpMessageHandler with only the features it supports, here are the defaults JsonHttpClient uses:

var client = new JsonHttpClient  {
    HttpMessageHandler = new HttpClientHandler
        //UseCookies = UseCookies,
        //CookieContainer = CookieContainer,
        //UseDefaultCredentials = Credentials == null,
        //Credentials = Credentials,
        //AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,

So I seem to be using the updated assembly because there is now a UseCookies property that I’m using as follows

var client2 = new JsonHttpClient("http://localhost:5004") { UseCookies = false };
var response = await client2.GetAsync(new Hello { Name = "servicestack" });

But I’m still getting the “Property UseCookies is not supported” error in the browser.

So perhaps the problem wasn’t the value of this property, but the presence of this property. Blazor WASM must not be liking it.

Yeah this is exactly the reason why I’d personally never develop Apps for crippled .NET Runtimes like Silverlight, WP, Xamarin.iOS & Blazor where only an unknown “safe subset” of .NET implementations is supported which you can never have any guarantee or degree of confidence that your compiled code will work at runtime or just thrown runtime exceptions & that you need to spend an eternity refactoring code to prod it to workaround the next minefield that pops up.

So here it’s not clear if it’s the presence of the UseCookies in compiled code or that it’s actually set. You can try using a custom HttpMessageHandler which will avoid the UseCookies property being set:

var client = new JsonHttpClient  {
    HttpMessageHandler = new HttpClientHandler {
        CookieContainer = CookieContainer,
        UseDefaultCredentials = Credentials == null,
        Credentials = Credentials,
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,

Let me know if it resolves it, or if any of the other properties are problematic in Blazor?

Well, it turns out I could only get it to work with the following

var client = new JsonHttpClient  {
    HttpMessageHandler = new HttpClientHandler {

Not sure the long term ramifications of not being able to specify any of those properties, but at a bare minimum, I’m able to call a remote ServiceStack API and get the data back I need in Blazor WASM using the above client.

Would it be possible to refactor JsonHttpClient somehow to create an HttpClientHandler without any of those properties being set? Would be much cleaner to not have to include the HttpMessageHandler code.

I’ll let you know if I run into any other issues. Thanks!

Blazor’s restricted .NET Runtime shouldn’t impact JsonHttpClient behavior, but there should be a future-proofed API that can be optimized later as more features that Blazor supports are known & enabled.

Which you can use the new BlazorClient factory for which basically wraps the above empty configuration:

var client = BlazorClient.Create(baseUrl);

Which should you need to, you can customize the MessageHandler all clients created with it will use:

BlazorClient.MessageHandler = new HttpClientHandler { ... };

Now available in the latest v5.8.1 on MyGet.

1 Like

Hi mythz,

the BlazorClient works well in my Blazor project. But I can´t store the cookie after the first authenticate requests. Can you give me a hint how to achive this?

            var client = BlazorClient.Create("https://localhost:5001");
            var response = await client.PostAsync(new Authenticate { UserName = "xxx", Password = "xxx", provider = "credentials" });

            var response2 = await client.GetAsync<SearchCustomersResponse>("https://localhost:5001/customers/favourites");

Thanks a lot

Doesn’t look like it works by default, (very little does as it’s primarily a wrapper around W3C fetch API), but this answer suggests you can set a switch to toggle the underlying fetch() to use credentials: 'include':

The solution is to use an HttpRequestMessage and set requestMessage.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);

Which you could initialize with a static global Request Filter, e.g:

JsonHttpClient.GlobalRequestFilter = req => 

Note this API is limited to Blazor package.