HttpTrigger Function - Access to User and Claims for JWT authenticated requests

Giuseppe Solino 40 Reputation points
2024-02-06T15:31:33.88+00:00

Hi everyone, thanks to the suggestion that was provided to me in this context, I managed to integrate Azure B2C security into my function. Now my HttpTrigger functions are protected by this authentication step! Now I have another problem. I would like to access, in the body of my function, the data of the authenticated user (HttpContext.User and HttpContext.User.Claims) to retrieve information such as username and claims associated from the JWT Bearer Token. I have seen some examples available online that decode the token and reconstruct it on the fly, but I would like to know if there is something already integrated that I can use perhaps by configuring it in the Program.cs file. Thanks again in advance Giuseppe

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,932 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2024-02-07T16:16:29.2033333+00:00

    Giuseppe Solino Thanks for posting your question in Microsoft Q&A. From the description above, you are using Easy Auth in Azure Functions with B2C and would like to access user info, claims info in the function code. The claims of the user/application authenticated are available in the HTTP request headers and you can access these headers in all language frameworks as described in Access user claims in app code doc and this applies to Azure Functions as well - refer Working with client identities doc. Then you need to decode the client principal header in your app code and since you mentioned C#, there is an example on how to parse the claims. User's image

    If you are using in-process model (or csx script), you can also pass ClaimsPrincipal as parameter (as suggested by Albert Tanure, like in this example) or grabbing it from the HttpRequest object via req.HttpContext.User. See this comment and Azure Functions HTTP trigger documentation and let me know if you have any questions or issues.

    In case of isolated model, req.Identities returns ClaimsIdentity collection (HttpRequestData.Identities) with few limitations. If it returns null for your scenario (or not yet supported), please feel free to submit feedback via https://github.com/Azure/azure-functions-dotnet-worker/issues.

    Note: ClaimsPrincipal.Current isn't populated automatically.

    I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Albert Tanure 0 Reputation points Microsoft Employee
    2024-02-07T09:19:10.56+00:00

    I believe you can add the ClaimsPrincipal as parameter of your function. You can read a bit more about this class on this link: ClaimsPrincipal class Kind Regards, Albert Tanure

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.