2

Azure http 触发函数应用程序不附带启动。我想实现 azure AD 身份验证,它添加了Microsoft.AspNetCore.Builder的UseAuthentication方法来验证令牌并对用户进行身份验证。

目前Http触发器是直接命中Run方法的。应该有一些中间件逻辑来添加服务和配置

创业班

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();
}

Authorize 属性下的 post implementation 应该验证令牌并允许/拒绝用户访问。

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");
       }
   }

请帮忙。

4

2 回答 2

2

Azure 函数默认没有Startup类。您可以使用 . 添加服务IWebJobStartup,但不能添加自定义中间件。

于 2019-11-11T07:17:38.063 回答
0

您可以[assembly: WebJobsStartup(typeof(MyNamespace.Startup))]注册和配置依赖注入绑定。参考这篇文章

[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...
        }
    }
}
于 2019-11-11T08:24:44.007 回答