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开始,以确保它配置正确

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...