我有一个Microsoft.Rest.ServiceClient
生成为autorest
。我想访问使用 Windows 身份验证和基本身份验证保护的 REST API。
目标是使用 Windows 身份验证。我尝试如下:
var handler = new HttpClientHandler
{
UseDefaultCredentials = true,
};
this.InitializeHttpClient(handler);
这不起作用,我得到:
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
当我使用基本身份验证时它可以工作。
this.Credentials = new BasicAuthenticationCredentials
{
UserName = Configuration.User,
Password = Configuration.Password
};
这个设置的ServiceClient
是在构造函数中完成的
MyClient : Microsoft.Rest.ServiceClient
我需要向客户端添加什么才能使 Windows 身份验证正常工作?
Edited:
看起来问题出在服务器端。 IIS 中的设置。
客户端将按预期工作。
这基本上重申了OP和@Anders已经涵盖的内容,用我喜欢的语法......
var windowsAuthHandler = new HttpClientHandler { UseDefaultCredentials = true };
var webApiUri = new System.Uri("https://localhost:8080");
var apiClient = new MyAutoRestClient(webApiUri ,windowsAuthHandler);
如果你浏览一下,OP似乎表明这不起作用,但实际上它确实有效。但是,正如 OP 后来指出的那样,请务必从 IIS 开始,以确保其配置正确
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)