Reputation: 8966
I'm adding an auth header token from my current request so I can hit another API, but when I add the AuthenticationHeaderValue
scheme (which cannot be blank), it duplicates "Bearer" in the token... so I just called Replace() on it to get it working...
var rawToken = this.HttpContext.Request.Headers["Authorization"];
var cleanedToken = rawToken.ToString().Replace("Bearer ", "", StringComparison.OrdinalIgnoreCase);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", cleanedToken);
HttpResponseMessage response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false);
But this call to Replace() feels like a dirty hack. Is there a battle-tested library method to handle this instead?
Upvotes: 5
Views: 1856
Reputation: 101583
Using AuthenticationHeaderValue.Parse(rawToken)
should do the job, as it will automatically extract "Bearer" scheme (or any other scheme) and token from the raw value, removing the need to pass it explicitly.
Upvotes: 8