How do I use Windows Authentication with the Flurl library?

4.5k Views Asked by At

Flurl has methods for doing OAuth and Basic authentication:

await url.WithBasicAuth("username", "password").GetJsonAsync();
await url.WithOAuthBearerToken("mytoken").GetJsonAsync();

but how do I do Windows authentication using the currently logged in user? The HttpClientHandler that Flurl is built on top of has a property UseDefaultCredentials but I don't know how to utilize that within Flurl.

var httpClient = new HttpClient(new HttpClientHandler() 
{
    UseDefaultCredentials = true
});
4

There are 4 best solutions below

0
On BEST ANSWER

Flurl intelligently reuses the HttpClientHandler for each domain, so you don't want to set the UseDefaultCredentials each time it runs. Instead, you can modify the HttpClientFactory to return one that's configured to UseDefaultCredentials.

public class UseDefaultCredentialsClientFactory : DefaultHttpClientFactory
{
    public override HttpMessageHandler CreateMessageHandler()
    {
        return new HttpClientHandler { UseDefaultCredentials = true };
    }
} 

Then you need to tell Flurl to use this factory for the domains you want to use Windows authentication for.

public static class FlurlConfiguration
{
    public static void ConfigureDomainForDefaultCredentials(string url)
    {
        FlurlHttp.ConfigureClient(url, cli =>
            cli.Settings.HttpClientFactory = new UseDefaultCredentialsClientFactory());
    }
}

Then you simply need to call this once on startup for each domain. For ASP.NET, the Application_Start method in your global application class is a good place for it.

FlurlConfiguration.ConfigureDomainForDefaultCredentials("https://example.com");
FlurlConfiguration.ConfigureDomainForDefaultCredentials("http://services.example.com");

Credit goes to Todd Menier for explaining this to me.

1
On

If it is still relevant. You can set credentials, something like this

 ((HttpClientHandler)url.Client.HttpMessageHandler).Credentials = new NetworkCredential(userName, password);
0
On

New answer for Flurl.Http 4.0. Factories are basically gone, and you can now configure things like this fluently. If you're using the clientless pattern, you can do this:

FlurlHttp.ConfigureClientForUrl("https://example.com")
    .ConfigureInnerHandler(hch => hch.UseDefaultCredentials = true);

If you're using DI with named clients, it'll look more like this:

services.AddSingleton<IFlurlClientCache>(sp => new FlurlClientCache()
    .Add("MyCli", "https://example.com", builder => builder
        .ConfigureInnerHandler(hch => hch.UseDefaultCredentials = true)));

In both cases you're configuring an IFlurlClientBuilder, a new type in 4.0 that's invoked (lazily) when a new client is created.

1
On

Create HttpClientHandler with your credentials and pass it to HttpClient then to FlurlClient like this:

var clientHandler = new HttpClientHandler
{
    Credentials = new NetworkCredential("admin", 
"bm8gcGFzc3dvcmQgaGVyZSB5b3UgZnVja2VyIQ==")
};
var httpClient = new HttpClient(clientHandler);
var client = new FlurlClient(httpClient);
var resp = await url.WithClient(client).GetAsync();