8

I have an Azure Function 2.x (Asp.net Core) and am authenticating with Azure AD. I'm trying to access the logged-in user's Claims after authentication. Previously using Azure Functions 1.x we would get the Claims using ClaimsPrincipal.Current, as seen in the code below:

using System.Net;
using System.Collections.Generic;
using System.Security.Claims; 
using Microsoft.IdentityModel.Clients.ActiveDirectory; 

public static HttpResponseMessage Run(HttpRequestMessage req, out object document, TraceWriter log)
{
    string name = ClaimsPrincipal.Current.FindFirst("name").Value; 

    log.Info($"name is {name}");

    return req.CreateResponse(HttpStatusCode.OK, "Done");
}   

Any guidance on how we access Claims in Azure Functions 2.x using .Net Core?

desflan
  • 468
  • 3
  • 13

2 Answers2

8

This feature is now supported in C# in Azure Functions 2.0. You can now add ClaimsPrincipal as a parameter to your HttpTrigger function's signature, or you can access it on the HttpRequest object via req.HttpContext.User.

Support should be coming soon to JavaScript, and eventually all languages should support this feature.

Connor McMahon
  • 1,318
  • 7
  • 15
  • Thanks Connor. In your link you mention it is possible in Functions V2 to get the ClaimsPrincipal of the current user as a parameter instead - can you tell me what you mean by this, as it may be a way to access claims in the meantime. Cheers – desflan Aug 08 '18 at 06:17
  • 2
    @desflan, that is the feature that we are currently adding. It is unfortunately not currently available, but expect it in the near future. – Connor McMahon Aug 08 '18 at 23:22
2

Let me just post a code example here, see where ClaimsPrincipal parameter is:

[FunctionName("MyFunctionName")]
public static HttpResponseMessage Run(
            [HttpTrigger(
                AuthorizationLevel.Anonymous,
                "get", "post",
                Route = "MyFunctionName")]HttpRequestMessage req, 
            ILogger log, 
            ClaimsPrincipal claimsPrincipal)
{
    // My function code here...
}
Dima G
  • 1,945
  • 18
  • 22