简体   繁体   English

如何在http触发器azure ZC1C425268E68385D14AB5074C17A9应用程序中添加自定义启动class

[英]How to add custom startup class in http trigger azure function app

Azure http trigger function apps doesn't comes with a startup. Azure http 触发器 function 应用程序没有启动。 I want to implement azure AD authentication which adds UseAuthentication method of Microsoft.AspNetCore.Builder to validate the token an authenticate the user.我想实现 azure AD 身份验证,它添加了Microsoft.AspNetCore.BuilderUseAuthentication方法来验证令牌并对用户进行身份验证。

Currently Http trigger is hitting the Run method directly.There should be some middle ware logic to add servies and configurations目前 Http 触发器直接命中 Run 方法。应该有一些中间件逻辑来添加服务和配置

Startup Class启动 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. Authorize 属性下的发布实现应该验证令牌并允许/拒绝用户访问。

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.请帮忙。

Azure function by default don't have a Startup class. Azure function 默认没有Startup class。 You can add services using an IWebJobStartup , but you cannot add custom middleware.您可以使用IWebJobStartup添加服务,但不能添加自定义中间件。

You could [assembly: WebJobsStartup(typeof(MyNamespace.Startup))] to register and configure the dependency injection bindings.您可以[assembly: WebJobsStartup(typeof(MyNamespace.Startup))]注册和配置依赖注入绑定。 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...
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM