如何在 Microsoft.Rest.ServiceClient 中使用 Windows 身份验证 [英] How can I use Windows Authentication with Microsoft.Rest.ServiceClient
问题描述
我有一个用 autorest
生成的 Microsoft.Rest.ServiceClient
.我想访问使用 Windows 身份验证和基本身份验证保护的 REST API.
I have a Microsoft.Rest.ServiceClient
generated with autorest
. And I want to access a REST API secured with Windows Authentication and Basic Authentication.
目标是使用 Windows 身份验证.我试过如下:
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
};
ServiceClient
的这个设置是在
MyClient : Microsoft.Rest.ServiceClient
我需要向客户端添加什么才能使 Windows 身份验证正常工作?
What do I need to add to the client to get Windows Authentication working?
已
看起来问题出在服务器端.IIS 中的设置.
It looks like the problem is on server side. Settings in IIS.
客户端会按预期工作.
推荐答案
这基本上重申了 OP 和 @Anders 已经涵盖的内容,以我喜欢的语法...
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);
如果您略读,OP 似乎表明这不起作用,但实际上确实如此.但是,正如 OP 稍后所述,请务必从 IIS 开始以确保其配置正确
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
这篇关于如何在 Microsoft.Rest.ServiceClient 中使用 Windows 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!