Enforce JsonServiceClient sending Authorization header

Hello,

We’re trying to call WEB API using JsonServiceClient. The IIS is configured with Windows Authentication and we’ve to provide credentials.
However, it seems that setting up Credentials property of the client isn’t enough. If we just setting Credentials, we see the client sending 2 requests instead of one. The first request is returned with 401. Then JsonServiceClient sends another request with Authorization header.

What is a way to enforce JsonServiceClient to send the request with this header in first attempt?

Thank you,
Leonid

The behavior you’re seeing is just how NTLM authentication works with .NET HttpWebRequest, where the JsonServiceClient just wraps a .NET HttpWebRequest to make the request.

You can find more info on how PreAuthenticate works with Windows Auth in this post:

It means that if I get to internal HttpWebRequest of the JsonServiceClient and set its PreAuthenticate property to true then any following JsonServiceClient instance will reuse the same credentials? Do you understand your answer correctly?

You can use a Global Request Filter to specify HttpWebRequest.PreAuthenticate = true for all Service Clients with:

ServiceClientBase.GlobalRequestFilter = req => req.PreAuthenticate = true;

This configures the client, but ultimately the behavior is up to the server.