Reputation: 85
I am trying to get the access_token and the claims of it from a request to an azure function.
The bearer token is set in the header but I am unable to get the claims of using the FunctionsStartup of the function.
Is it possible to get the claims in an azure function? If yes please someone can provide an example?
I was trying all the day, also finding but i couldn't find a clear example.
Thanks in advance!
EDIT:
When I add Authorization in the FunctionsStartup of the function appear the next error. builder.Services.AddAuthorization()
I have configured the authentication by JwtBearer: services.AddAuthentication("Bearer").AddJwtBearer((Action)
Upvotes: 2
Views: 4171
Reputation: 11
I was having some issues getting the claims from my Azure function too. I ended up getting them from the HttpRequestMessage object itself, like this:
var principal = req.GetRequestContext().Principal as ClaimsPrincipal;
var roles = principal.Claims.Where(e => e.Type == "roles").Select(e => e.Value); log.Info(string.Join(",", roles));
Please do notice I was using Microsoft.NET.Sdk.Functions version 1.0.37, not sure if this applies for your scenario but you can give it a try.
Microsoft has stated that ClaimsPrincipal.Current should not be used, so that option probably will not work for you https://learn.microsoft.com/en-us/aspnet/core/migration/claimsprincipal-current?view=aspnetcore-2.1
Upvotes: 1
Reputation: 65391
You can do this by using the Claims Principle binding for Azure functions.
using System.Net;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
public static IActionResult Run(HttpRequest req, ClaimsPrincipal principal, ILogger log)
{
// ...
return new OkResult();
}
See: http://sadomovalex.blogspot.com/2018/12/get-current-users-principal-in-azure.html
Upvotes: 1