微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在Microsoft.Rest.ServiceClient中使用Windows身份验证

我有一个使用autorest生成的Microsoft.Rest.ServiceClient.我想访问使用 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身份验证正常工作?

编辑:

看起来问题出在服务器端. 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开始,以确保它配置正确

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐