Last post Jun 29, 2015 02:31 AM by Rocky Web API
Jun 25, 2015 03:43 PM|Robert Johnston|LINK
I need some help with making a call to an API that is using basic authentication. I have found a lot on how to do it on the controller side of the equation, but I am unable to find something to how you would make the call and pass the credentials.
Any help is greatly appreciated,
Jun 25, 2015 07:30 PM|santhoshje|LINK
Please generate a Base64 string using username and password. Then include it in request header like this
using (var client = new HttpClient())
client.BaseAddress = new Uri("http://localhost:59150/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(userName + ':' + password)));
var response = client.GetAsync("api/values/10").Result;
string responseString = response.Content.ReadAsStringAsync().Result;
Jun 29, 2015 02:31 AM|Rocky Web API|LINK
General, we add custom header for authentication.
In the server site, We verify the header in Controller or DelegatingHandler or something else.
IEnumerable<string> AuthenticationToken = null;
request.Headers.TryGetValues("HeaderName", out AuthenticationToken);
Hope it helps.