在使用webapi时,我收到401错误(由于凭据无效而拒绝访问) [英] I am getting a 401 error(Access denied due to invalid credentials), when working with webapi

查看:1107
本文介绍了在使用webapi时,我收到401错误(由于凭据无效而拒绝访问)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的计算机上的控制台应用程序使用服务器上的webapi文件,我们已经启用了Windows身份验证。

我真的很感激,如果有人可以提供帮助,因为我从昨天开始尝试并寻找答案并尝试了不同的方法。



< pre lang =C#> client.BaseAddress = new Uri(baseurl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( 应用/ JSON));
int anId = 100 ;
HttpResponseMessage响应;
string serviceUrl = api / contacts ;

// var Id ={'Id':'0001'};

var anContact = new 联络()
{
Id = 600
ContactName = Nick
ContactPhNo = 789-665-345
};



// 2.获取所有员工。

Console.WriteLine( 2.通过发送GET {0}获取所有员工/ {1} \\\\ n,baseurl,serviceUrl);

response = client.GetAsync(serviceUrl).Result;

解决方案

您好,



为了使用Windows访问WebAPI身份验证,您必须创建一个HttpClientHandler并在其中设置凭据。然后,必须将此HttpClientHandler对象传递给HttpClient。



有关详细说明,请参阅我的技术提示。



使用基本和Windows身份验证验证WebAPI [ ^ ]



希望这会有所帮助。快乐学习!!


I am trying to consume the webapi file which is on server with the console application on my machine, We have windows authentication on.
I would really appreciate if someone could help as I am trying since yesterday and searching for the answer and tried different methods too.

client.BaseAddress = new Uri(baseurl);
           client.DefaultRequestHeaders.Accept.Clear();
           client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
           int anId = 100;
           HttpResponseMessage response;
           string serviceUrl = "api/contacts";

           //var Id = "{'Id':'0001'}";

           var anContact = new Contact()
           {
               Id=600,
               ContactName = "Nick",
               ContactPhNo = "789-665-345"
           };

           

           // 2. Get all employees.

           Console.WriteLine("2. Get All Employee by sending GET {0}/{1} \r\n", baseurl, serviceUrl);

           response = client.GetAsync(serviceUrl).Result;        

解决方案

Hi,

In order to access a WebAPI using windows authentication, you have to create a HttpClientHandler and set the credentials in it. Then, this HttpClientHandler object has to be passed to the HttpClient.

Please refer my technical tip on this for detailed explanation.

Authenticate WebAPIs with Basic and Windows Authentication[^]

Hope this helps. Happy learning!!


这篇关于在使用webapi时,我收到401错误(由于凭据无效而拒绝访问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆