2

Azure http trigger function apps doesn't comes with a startup. I want to implement azure AD authentication which adds UseAuthentication method of Microsoft.AspNetCore.Builder to validate the token an authenticate the user.

Currently Http trigger is hitting the Run method directly.There should be some middle ware logic to add servies and configurations

Startup Class

public void ConfigureServices(IServiceCollection services)
{            services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
               .AddAzureADBearer(options => Configuration.Bind("ConfigName", options));
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger)
{
   app.UseAuthentication();
}

post implementation below Authorize attribute should validate the token and allow/deny the user access.

public static class Function1
   {
       [Authorize]
       [FunctionName("Function1")]
       public static async Task<IActionResult> Run(
           [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
           ILogger log)
       {
           log.LogInformation("C# HTTP trigger function processed a request.");

           return (ActionResult)new OkObjectResult($"Hello");
       }
   }

Please help.

Sharon
  • 93
  • 1
  • 13
  • were you able to solve this issue? I have the same problem, I want to call my middleware library by calling `app.UseAuthentication()`. Any help would be much appreciated – Sri Reddy Mar 17 '22 at 18:42

2 Answers2

3

Azure function by default don't have a Startup class. You can add services using an IWebJobStartup, but you cannot add custom middleware.

Rain336
  • 1,450
  • 14
  • 21
0

You could [assembly: WebJobsStartup(typeof(MyNamespace.Startup))] to register and configure the dependency injection bindings. Refer to this article.

[assembly: WebJobsStartup(typeof(MyNamespace.MyStartup))]
namespace MyNamespace
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            return (ActionResult)new OkObjectResult($"Hello");
        }
    }
    public class MyStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
        builder.Services...
        }
    }
}
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • IWebJobsStartup have only 1 method to implement. this is not accepting IApplicationBuilder ` namespace Microsoft.Azure.WebJobs.Hosting { public interface IWebJobsStartup { void Configure(IWebJobsBuilder builder); } }` – Sharon Nov 12 '19 at 09:34
  • Sorry for my mistake. I think the fact that there is not way in the Azure Functions Startup to invoke the app.useAuthentication call used in the MVC Web API that adds the authentication middleware, is the underlying reason. Refer to this [issue](https://github.com/Azure/azure-functions-host/issues/4006#issuecomment-495933900). – Joey Cai Nov 12 '19 at 09:55
  • There is an IStartup under Microsoft.AspNetCore.Hosting which has a definition for void Configure(IApplicationBuilder app); but the challenge now is startup is not picking.usually asp.net core applications should check for static void main, but here its directly hitting run method of trigger. – Sharon Nov 12 '19 at 12:41
  • Yes, in Asp.net core the startup.cs could contain `IApplicationBuilder `.There’s startup class [supported by Azure Functions V2](https://github.com/Azure/azure-functions-dotnet-extensions/blob/master/src/Extensions/DependencyInjection/FunctionsStartup.cs). It’s similar to one we know from ASP.NET Core but **there are differences**. Refer to this [article](https://gunnarpeipman.com/azure-functions-dependency-injection/). – Joey Cai Nov 18 '19 at 06:52
  • I have one doubt Can I use Autofac for DI in startup class? I followe this link for DI. http://dontcodetired.com/blog/post/Azure-Functions-Dependency-Injection-with-Autofac – Shrirang Feb 28 '20 at 05:21