Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Extension method to extract custom header value from Web API request
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
public static class HttpRequestMessageExtensions
public static string GetHeaderValue(this HttpRequestMessage request, string name)
IEnumerable<string> values;
var found = request.Headers.TryGetValues(name, out values);
if (found)
return values.FirstOrDefault();
return null;

Could there be a problem though with another request coming in and it's on the same thread? In other words, if I was trying to grab a token from the header so I could then convert that to an account ID, could I possibly get the other user's account if they also hit the REST API at the same time? I ran into this when using the following code inside a controller's method:

account_id = HttpContext.Current.Request.Headers.Get("account_id")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment