将 OpenIdConnect 与 AzureFunctions 结合使用
Using OpenIdConnect with AzureFunctions
我正在使用 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 接口创建 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 服务器不正确,而不是我的客户端)
这不是最好的解决方案,但在 MS 支持之前,这个 nuget 可以完成这项工作。我刚刚测试过,对我来说效果很好
https://github.com/kuromukira/azure-functions-jwt-validation-extension
我正在使用 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 接口创建 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 服务器不正确,而不是我的客户端)
这不是最好的解决方案,但在 MS 支持之前,这个 nuget 可以完成这项工作。我刚刚测试过,对我来说效果很好
https://github.com/kuromukira/azure-functions-jwt-validation-extension