If I have a method that sends some data to an endpoint, I understand I should use a bearer token to authenticate this call, sent in the header of the request.
Say my method that sends/receives data to/from the endpoint looks like this:
public async Task<string> PostGetAsync()
{
var uri = new Uri("https://localhost:44322/endpoint");
using (var client = new HttpClient())
{
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Key", "Value")
};
var content = new FormUrlEncodedContent(pairs);
var response = await client.PostAsync(uri, content);
if (response.StatusCode != HttpStatusCode.OK)
{
return "Error posting KeyValue";
}
string responseString = response.Content.ReadAsStringAsync().Result;
JArray json = JArray.Parse(responseString);
try
{
var returnedJson = json[returnedData];
return returnedJson.ToString();
}
catch (Exception e)
{
return "Index is out of bounds";
}
}
}
And the method that runs when that endpoint is called it this:
public async Task<JsonResult> endpoint()
{
List<Example> items = new List<Example>();
NameValueCollection nvc = Request.Form;
string keyString = nvc["Key"];
try
{
items = await GetService.GetList(keyString);
}
catch (ServiceException se)
{
}
return Json(items, JsonRequestBehavior.AllowGet);
}
How do I:
- Send a bearer token (custom stored in azure keyvault) to the endpoint.
- Validate this token from the endpoint
I can't find any beginner friendly docs for doing this.