I am creating the ASP.NET which is calling a third party Rest API. The third party API can be accessed only giving username and password. Also uses Http Basic Authentication
public string Get(string LabName)
{
string userName = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
string BaseURL = ConfigurationManager.AppSettings["BaseURL"];
using (var client = new HttpClient())
{
Uri uri = new Uri(BaseURL);
client.BaseAddress = uri;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
string clarity_URL = BaseURL+"api/v2/labs?name="+LabName;
var response = client.GetAsync(clarity_URL).Result;
string responseString = response.Content.ReadAsStringAsync().Result;
return responseString;
Should I be using HttpClient
or HttpWebRequest
.I am not sure how to pass the username and password while calling the Rest API in the ASP.NET.Can anyone suggest me how to securely call the API
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.