none
Webclient login RRS feed

  • Question

  • Hi all,

    i am trying to call a REST api client (zabbix) from a WPF application. I am trying to achieve this using WebClient.

    But the thing is the REST api needs login credentials to login and establish connection after which i can make my requests. Is there any way that i can achieve this using WebClient?? 

    Friday, April 3, 2020 5:37 AM

Answers

  • You shouldn't be using WebClient with a REST API. WebClient is really designed for working with old-style HTML services. HttpClient has been the preferred approach for years. HttpClient has a property exposing called DefaultRequestHeaders that exposes the default headers to send with every request. On this type is the Authorization header which is how you set authentication information for the request. .NET ships with a couple of classes to wrap common authorization schemes including basic.

    //Done at app startup or during lazy initialization
    _client = new HttpClient { BaseAddress = new Uri("https://tempuri.org") };
    client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue("user", "password");
    
    //Use the client later in the app
    using (var response = await _client.GetAsync("resource"))
    {
       ...
    };
    
    //Simple helper class for basic auth
    public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
    {
       public BasicAuthenticationHeaderValue ( string userName, string password ) : base("Basic", Encode(userName, password))
       {
       }
    
       private static string Encode ( string userName, string password )
       {
          var value = userName + ":" + password;
          return Convert.ToBase64String(Encoding.Default.GetBytes(value));
       }
    }

    Note that you should create the HttpClient only once in your app and reuse the instance for the life of your app (google for the many reasons why). You must set the default headers only the first time you create the client. Therefore the client will always use the same authentication. If you need different authentication for each request (rare and questionable) then you'll need to instead set the authorization header on each request via the HttpRequestMessage directly.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Abijithg92 Friday, April 24, 2020 3:29 PM
    Friday, April 3, 2020 1:53 PM
    Moderator

All replies

  • Hi Abijithg92,

    Thank you for posting here.

    We can use HTTP Basic authentication or OAuth authentication to connect to a REST API. I don't know which way the API you are using supports.

    Please check the links below.

    Calling a rest api with username and password

    Authentication with the Workflow REST API

    By the way, you can ask questions about WPF at Microsoft Q&A, they are more familiar with WPF.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 3, 2020 9:51 AM
  • You shouldn't be using WebClient with a REST API. WebClient is really designed for working with old-style HTML services. HttpClient has been the preferred approach for years. HttpClient has a property exposing called DefaultRequestHeaders that exposes the default headers to send with every request. On this type is the Authorization header which is how you set authentication information for the request. .NET ships with a couple of classes to wrap common authorization schemes including basic.

    //Done at app startup or during lazy initialization
    _client = new HttpClient { BaseAddress = new Uri("https://tempuri.org") };
    client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue("user", "password");
    
    //Use the client later in the app
    using (var response = await _client.GetAsync("resource"))
    {
       ...
    };
    
    //Simple helper class for basic auth
    public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
    {
       public BasicAuthenticationHeaderValue ( string userName, string password ) : base("Basic", Encode(userName, password))
       {
       }
    
       private static string Encode ( string userName, string password )
       {
          var value = userName + ":" + password;
          return Convert.ToBase64String(Encoding.Default.GetBytes(value));
       }
    }

    Note that you should create the HttpClient only once in your app and reuse the instance for the life of your app (google for the many reasons why). You must set the default headers only the first time you create the client. Therefore the client will always use the same authentication. If you need different authentication for each request (rare and questionable) then you'll need to instead set the authorization header on each request via the HttpRequestMessage directly.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Abijithg92 Friday, April 24, 2020 3:29 PM
    Friday, April 3, 2020 1:53 PM
    Moderator