I am building an Azure Functions v3 .NET Core 3.1 app with an http trigger. I have created function that retrieves a jwt from the request’s Authorization header and validates it with JwtSecurityTokenHandler
. This works if I create a startup class that inherits from FunctionsStartup. However if I inherit from WebJobStartup I run into an error. The app cannot find Microsoft.Extensions.Primitives.StringValues Microsoft.AspNetCore.Http.IHeaderDictionary.get_Item
. I can step through the code, but any function that tries access an entry in the request.Headers
dictionary doesn’t get executed and produces the error (screenshot below).
Here is the code. The debugger won’t step into the GetJwtFromHeader
function if the req.Headers
reference is there :
[FunctionName("HttpAuth")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ExecutionContext context,
ILogger log)
{
SomeFunction();
var jwt = GetJwtFromHeader(req);
}
private string GetJwtFromHeader(HttpRequest req)
{
var authorizationHeader = req.Headers?["Authorization"];
string[] parts = authorizationHeader?.ToString().Split(null) ?? new string[0];
return (parts.Length == 2 && parts[0].Equals("Bearer")) ? parts[1] : string.Empty;
}
The debugger never goes into theGetJwtFromHeader
function. If I remove the reference to req.Headers, then the debugger will step into it. It seems that any function trying to access the Header dictionary via code can not find it. However, if I inspect the object in the watch window, I see the correct value.
Could this be a problem with versioning in my packages? I tried to install a different version of Microsof.Extensions.Primitives
but I’m having problems downgrading to anything below 5.0. Why am I having problems with the Header dictionary?
I figured out the problem. In a project I was importing I was using Microsoft.Extensions.Configuration to read entries out of my secrets and for some reason version 5.0.0 wasn't playing well with my Azure functions project. I downgraded this to 3.1.13 and it worked.