.NET 7 MAUI HttpClient and NTLM authentication 401

332 Views Asked by At

We are developing a .NET 7 MAUI app that needs to access a WEB API that requires NTLM Authorization. The app works for Windows and IOS, but Android always returns a 401 error. The web API works with a tool like Insomnia. It works for webpages that were developed. As I said, it only continually fails for all versions of Android. Now that I type this, I think that might be my fix... change the version of Android. Anyways, here is the test code I am using that always fails with a 401 error.

private async Task TestHttpClient() {

    var uri = new Uri("https://mywebsite.domain.com/apifnc");

    try
    {
        // Create a new Credential - note NTLM is not documented but works
        var credentialsCache = new CredentialCache();
        credentialsCache.Add(uri, "NTLM", new NetworkCredential("username", "password", "domain"));

        

        var handler = new HttpClientHandler() { Credentials = credentialsCache, PreAuthenticate = true, UseDefaultCredentials = false };
        var httpClient = new HttpClient(handler) { Timeout = new TimeSpan(0, 0, 10) };
        //httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


        var response = await httpClient.GetAsync(uri);

        var result = await response.Content.ReadAsStringAsync();

        Debug.WriteLine(result);
        return result;
    }
    catch(Exception ex)
    {
        Debug.WriteLine(ex.Message);
        return string.Empty;
    }

    
}
0

There are 0 best solutions below