我正在使用azure函数来托管react应用程序的API,但是我也在使用相同的azure函数来托管应用程序的html/js/css(通过代理函数对Blob存储上的静态文件进行处理)。

我一直在使用EasyAuth为其提供身份验证,该身份一直很好地工作,但是我需要支持EasyAuth中未内置的身份提供程序(并且它根本不支持自定义身份提供程序)。这意味着我回退到使用Microsoft.AspNetCore.Authentication.OpenIdConnect程序包。

我已经在启动文件中注册了身份验证

 builder.Services
            .AddAuthentication()
            .AddCookie("WebJobsAuthLevel") //errors without this, although I suspect it's wrong
            .AddCookie("Bearer") //errors without this, although I suspect it's wrong
            .AddOpenIdConnect("custom", o =>
            {
                o.MetadataAddress = "https://localhost:44320/.well-known/openid-configuration";
                o.ClientId = "clientid";
                o.ClientSecret = "secret";
                o.ResponseMode = OpenIdConnectResponseType.Code;
                o.SignInScheme = "Cookies";
                o.GetClaimsFromUserInfoEndpoint = true;
             });

以及让我触发挑战的功能
    [FunctionName("CustomAuth")]
    public async Task<IActionResult?> Challenge([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = ".auth/login/custom")]HttpRequest req, ILogger log)
    {
        return new ChallengeResult("custom");
    }

如果我按下此功能,则效果很好,可以重定向到身份验证提供程序进行登录。

但是,一旦我登录,它就会重定向回我的功能应用程序,即404的

http://localhost:7071/signin-oidc?querystringhere

在这个阶段,我猜测AddAuthentication无法像在asp.net mvc核心中使用它一样,可以挂接到传入的Web请求中。想知道我是否可以通过较低级别或通过自定义的Azure函数将其连接起来

最佳答案

  • 添加对Microsoft.Azure.WebJobs.Script.WebHost的引用
  • 使用相对较新的IJobHostHttpMiddleware接口(interface)
  • 创建AzureFunctions中间件
  • 将此中间件注册为服务
  • 
        public class AzureFunctionsAuthenticationMiddleware : IJobHostHttpMiddleware
        {
            private IAuthenticationSchemeProvider _schemeProvider;
    
            public AzureFunctionsAuthenticationMiddleware(IAuthenticationSchemeProvider schemeProvider)
            {
                _schemeProvider = schemeProvider;
            }
    
            public Task Invoke(HttpContext context, RequestDelegate next)
            {
                return new AuthenticationMiddleware(next, _schemeProvider).Invoke(context);
            }
        }
    
      public void Configure(IWebJobsBuilder builder)
            {
                builder.Services.AddHttpContextAccessor();
    
    
                builder.Services.AddSingleton();
    
                builder.Services.AddAuthentication(options =>
                {
                    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                })
                .AddArmToken()
                .AddScriptAuthLevel()
                .AddScriptJwtBearer()
                .AddCookie()
                .AddOpenIdConnect("custom", o =>
                {
                    o.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;
                    o.SignInScheme = "Cookies";
    
                    o.MetadataAddress = "metadata address";
                    o.ClientId = "clientid";
                    o.ClientSecret = "secret";
                    o.ResponseMode = "query";
                    o.ResponseType = "code";
                });
    

    这解决了signin-oidc 404的问题,我现在遇到另一个不确定不确定的openid消息的问题,该消息我不确定是相关的(例如,我认为我的openidconnect服务器不正确,而不是我的客户端不正确)

    关于azure-functions - 将OpenIdConnect与AzureFunctions一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57404968/

    10-09 18:12
    查看更多