加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:50:42 所属栏目:百科 来源:网络整理
导读:我有一个使用autorest生成的Microsoft.Rest.ServiceClient.我想访问使用 Windows身份验证和基本身份验证保护的REST API. 目标是使用Windows身份验证.我尝试了如下: var handler = new HttpClientHandler{ UseDefaultCredentials = true,};this.InitializeHt
我有一个使用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开始,以确保它配置正确

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读