简体   繁体   English

使用基本身份验证连接到Restful Web服务

[英]Connecting to a Restful Web Service using Basic Authentication

I am trying to make a request from a RWS and I am constantly getting the 401 not authorized error 我正在尝试从RWS发出请求,并且不断收到401 not authorized error

I have been provided a sample username and password for testing purposes but I have a feeling my implementation is off. 我已经提供了用于测试目的的示例用户名和密码,但是我感觉我的实现已关闭。 This is my first foray into the world of RWS. 这是我首次进入RWS领域。

class Program
    {
        static string _address = "https://this.istheservice/";

        static async void RunClient()
        {
            HttpClientHandler handler = new HttpClientHandler();

            handler.UseDefaultCredentials = false;

            handler.Credentials = new NetworkCredential("Sample", "P@ssw0rd!!");

            HttpClient client = new HttpClient(handler);

            // Send asynchronous request
            HttpResponseMessage response = await client.GetAsync(_address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();//Breaks here

When I inspected the NetworkCredentials the Domain was listed as Domain = "" would this cause an issue? 当我检查NetworkCredentials时,该域被列为Domain = "" ,这会引起问题吗?

If I drop the URL they provide into a browser I get 401 error as well. 如果我将它们提供的URL放到浏览器中,我也会收到401错误。

If more information is needed please ask, I am not sure what moving parts need to be included here. 如果需要更多信息,请询问,我不确定此处需要包括哪些运动部件。

I am working from this example: http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx and I am using Fiddler to monitor the requests. 我正在使用以下示例: http : //blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx,并且我正在使用Fiddler监视请求。

What your trying to do there is not basic authentication. 您在那里尝试做的不是基本身份验证。 To do basic Authentication you need to base64 encrypt username:password and put them in the header 要进行基本身份验证,您需要对base64加密username:password并将其放在标题中

   string basic = "Basic ENCRYPTEDSTRING";
   HttpClient client = new HttpClient();
   client.DefaultRequestHeaders.Add("Authorization", basic);

So your header should have something like this when you see it in fiddler 因此,当您在提琴手中看到标题时,标题应该具有类似的内容

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 授权:基本QWxhZGRpbjpvcGVuIHNlc2FtZQ ==

Wiki link to basic authentication http://en.wikipedia.org/wiki/Basic_access_authentication 指向基本身份验证的Wiki链接http://en.wikipedia.org/wiki/Basic_access_authentication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM