Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Windows Authentication with Microsoft.Rest.ServiceClient

I have a Microsoft.Rest.ServiceClient generated with autorest. And I want to access a REST API secured with Windows Authentication and Basic Authentication.

The goal is to use Windows Authentication. I tried it as follows:

var handler = new HttpClientHandler
{
    UseDefaultCredentials = true,
};
this.InitializeHttpClient(handler);

This does not work, I get:

System.Net.Http.HttpRequestException: An error occurred while sending the request. 
---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. 
---> System.ComponentModel.Win32Exception: The target principal name is incorrect

When I use Basic Authentication it works.

this.Credentials = new BasicAuthenticationCredentials
{
    UserName = Configuration.User,
    Password = Configuration.Password
};

This setup of the ServiceClient is done in the constructor of

MyClient : Microsoft.Rest.ServiceClient

What do I need to add to the client to get Windows Authentication working?

Edited:

It looks like the problem is on server side. Settings in IIS.

The client would work as expected.

like image 210
Heiner Avatar asked Apr 18 '18 14:04

Heiner


1 Answers

This basically reiterates what's already covered in the OP and by @Anders, in my preferred syntax...

 var windowsAuthHandler = new HttpClientHandler { UseDefaultCredentials = true };
 var webApiUri = new System.Uri("https://localhost:8080");
 var apiClient = new MyAutoRestClient(webApiUri ,windowsAuthHandler);

If you're skimming, the OP seems to indicate this doesn't work, when, indeed it does. But, as the OP later states, be sure to start with IIS to make sure it's configured right

like image 147
bkwdesign Avatar answered Oct 13 '22 09:10

bkwdesign