3

I have delegated Bearer Token Validation to Azure Function Invocation Filter. It validates the token and gets the claims. Now i'm trying to pass those claims or ClaimsPrincipal object into Function.

     public override Task OnExecutingAsync(
                FunctionExecutingContext executingContext, CancellationToken cancellationToken)
     {
             var handler = new JwtSecurityTokenHandler();
             handler.InboundClaimTypeMap.Clear();
             principal = handler.ValidateToken(jwtToken,
                    new TokenValidationParameters
                    {
                        ValidateAudience = false,
                        ValidIssuer = issuer,
                        ValidateIssuerSigningKey = false,
                        SignatureValidator = (t, param) => new 
                                                        JwtSecurityToken(t),
                        NameClaimType = "sub"

                    }, out var token);
     }

I see a dictionary called Properties in FunctionExecutingContext. But as per the documentation it is used for passing data between filters.

So executingContext.Properties["claims"] = principal; doesn't pass data to function.

We could achieve it with RouteData in WebAPI but not sure if it's possible to do it in Azure Functions. Any help is highly appreciated.

Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90

2 Answers2

0

Your options are a bit limited -there's no generic property bag plumbed through like Request.Properties. You could have the Filter be on a class with your functions, and then the filter can set instance fields when it executes; and your function can read the fields. You may need to set a JobActivator and ensure that each invocation gets a new class instance.

Mike S
  • 3,058
  • 1
  • 22
  • 12
  • Thanks. I am using Azure Functions. classes are static. so i think it's not possible to have new class instance each time a function is called. – Venkata Dorisala Oct 17 '17 at 06:43
0

I don't know if you found a solution already, but there is a solution when you are using the function as http trigger. The first argument of you're function will be a HttpRequestMessage object (req). Retrieve this argument in the filter like this:

HttpRequestMessage req = (HttpRequestMessage)executingContext.Arguments.First().Value;

You can use req.Properties to add custom data and read this from inside your function.

Jonathan Anctil
  • 1,025
  • 3
  • 20
  • 44