Invoking a Web API with Basic Authentication in C#
Dec 29, 2020 07:30 0 Comments C# (C-Sharp) PARTH

        Invoking a Web API with Basic Authentication in C#  

In this blog, we will discuss on how to call an ASP.NET Web API which requires the username and password (Basic Authentication) for authentication.   In the below example, we will call our API using Console Application in C# -

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Net.Http; using System.Text;

using System.DirectoryServices;

using System.Configuration;

using System.DirectoryServices.AccountManagement;

using System.Management.Automation.Runspaces;

using System.Management.Automation;

using System.Diagnostics;

namespace MSTeams.MobileAdminRules.Console

{    

class Program  

   {      

   public static HttpClient webClient = new HttpClient();       

  static void Main(string[] args)        

{        

     Program program = new Program();      

       program.ShowRecords();  

       }       

  private void ShowRecords()  

       {          

   HttpMessageHandler handler = new HttpClientHandler()        

     {       

      };          

     var httpClient = new HttpClient(handler)        

     {       

          //BaseAddress = new Uri("https://mcportal.accenture.com/WebTools/MobileAdminRulesWebAPI/api/AdminRulesWebAPI/")                 BaseAddress = new Uri("http://localhost:57298/api/AdminRulesWebAPI/")          

   };

              httpClient.DefaultRequestHeaders.Add("ContentType", "application/json");      

         var plainTextBytes = Encoding.UTF8.GetBytes("parth.jolly@accenture.com:*********"); //put here complete Accenture email ID and password             string credentials = Convert.ToBase64String(plainTextBytes);   

          httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + credentials);     

          var method = new HttpMethod("GET");      

         //Test Case 1             System.Console.WriteLine("+++++++++++++++++++++++++++++++++++Test Case One+++++++++++++++++++++++++++++++++++++++++");             

  HttpResponseMessage responseOne = httpClient.GetAsync("GetData").Result;           

  IEnumerable contentOne = new List();   

          contentOne = responseOne.Content.ReadAsAsync>().Result;  

            foreach (MobilityAdminRule data in contentOne)         

    {            

     System.Console.WriteLine("GU : " + data.GU + "\n" + "Country Code :

" + data.CountryCode + "\n" + "Cost Center Code : " + data.CostCenterCode + "\n" + "Job Family Code : " + data.JobFamilyCode + "\n" + "Service Level With Voice : " + data.ServiceLevelWithVoice + "\n" + "Service Level Without Voice : " + data.ServiceLevelWithoutVoice);    

             System.Console.WriteLine("-------------------------------------------------------------------------------------");     

        }           

 

    //Test Case 2             System.Console.WriteLine("+++++++++++++++++++++++++++++++++++Test Case Two++++++++++++++++++++++++++++++++++++++++++");        

       HttpResponseMessage responseTwo = httpClient.GetAsync("GetVoiceNonVoiceData/AFR").Result;      

       IEnumerable contentTwo = new List();    

         contentTwo = responseTwo.Content.ReadAsAsync>().Result;         

      foreach (MobilityAdminRule data in contentTwo)   

          {    

             System.Console.WriteLine("GU : " + data.GU + "\n" + "Country Code : " + data.CountryCode + "\n" + "Cost Center Code : " + data.CostCenterCode + "\n" + "Job Family Code : " + data.JobFamilyCode + "\n" + "Service Level With Voice : " + data.ServiceLevelWithVoice + "\n" + "Service Level Without Voice : " + data.ServiceLevelWithoutVoice);         

    }       

        //Test Case 3             System.Console.WriteLine("+++++++++++++++++++++++++++++++++++Test Case Three++++++++++++++++++++++++++++++++++++++++");               HttpResponseMessage responseThree = httpClient.GetAsync("GetMobilityServiceLevelToUse/parth.jolly").Result;   

         dynamic contentThree = responseThree.Content.ReadAsStringAsync().Result;        

       System.Console.WriteLine(contentThree);  

            //Test Case 4             System.Console.WriteLine("+++++++++++++++++++++++++++++++++++Test Case Four+++++++++++++++++++++++++++++++++++++++++");        

       HttpResponseMessage responseFour = httpClient.GetAsync("GetMobilityServiceLevelToUse/englebert.c.calaro").Result;     

        dynamic contentFour = responseFour.Content.ReadAsStringAsync().Result;      

         System.Console.WriteLine(contentFour);

              //Test Case 5             System.Console.WriteLine("+++++++++++++++++++++++++++++++++++Test Case Five++

++++++++++++++++++++++++++++++++++++++++");              

responseFive = httpClient.GetAsync("GetMobilityServiceLevel/\"nithushree.cs\"").Result;    

         dynamic contentFive = res

ponseFive.Content.ReadAsStringAsync().Result;     

          System.Console.WriteLine(contentFive);            

   System.Console.ReadLine();  

       }

    }

}      

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect