Search code examples
c#azureazure-functionsazure-webjobsazure-webjobssdk

How to get the current Azure FunctionNameAttribute?


How do I access the function name set with [FunctionName("SomeFunctionName")] programmatically inside the function?

This is the function attribute name and not the method name.

eg: In the code below, I would like to do something like if (!await PermissionsHelper.userHasPermission(getCurrentFunctionAttributeName(), TokenHelper.getTokenValue(req, "oid")))


  public static class GetUser
  {
    [FunctionName("GetUser")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "users/{id}")]
          HttpRequest req,
          String id,
          ILogger log)
    {

      if (!await PermissionsHelper.userHasPermission("GetUser", TokenHelper.getTokenValue(req, "oid")))
        return new UnauthorizedResult();


Solution

  • try the following:

    public static class GetUser
    {
      [FunctionName("GetUser")]
      public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "users/{id}")]
          HttpRequest req,
          String id,
          ExecutionContext executionContext,
          ILogger log)
      {
    
        if (!await PermissionsHelper.userHasPermission(executionContext.FunctionName, TokenHelper.getTokenValue(req, "oid")))
        return new UnauthorizedResult();