在 ASP.NET Core 中使用 Sustainsys.Saml2 动态添加 SAML2 身份验证提供程序

Dynamically add a SAML2 authentication provider using Sustainsys.Saml2 in ASP.NET Core

我正在尝试在 ASP.NET Core 和 Sustainsys.Saml2 库中使用 IAuthenticationSchemeProvider 动态添加 SAML2 身份验证方案:

schemeProvider.AddScheme(new AuthenticationScheme("myAuthScheme", "myAuthScheme", typeof(Saml2Handler)));

除了方案,我还需要配置与之配套的Saml2Options。我正在尝试使用 IOptionsMonitorCache<Saml2Options> 这样做:

samlOptionsCache.TryAdd("myAuthScheme", options);

当我尝试使用此方案进行身份验证时,出现以下错误:

NullReferenceException: Object reference not set to an instance of an object. Sustainsys.Saml2.WebSso.Saml2Urls..ctor(HttpRequestData request, IOptions options) Sustainsys.Saml2.WebSso.SignInCommand.Run(EntityId idpEntityId, string returnPath, HttpRequestData request, IOptions options, IDictionary relayData) Sustainsys.Saml2.AspNetCore2.Saml2Handler.ChallengeAsync(AuthenticationProperties properties)

所以看起来这些属性从未与该方案相关联。

我不确定我这样做是否正确。这样动态注册scheme可以吗?

事实证明,只有记录器没有被实例化,其他所有选项都很好。我通过添加...

解决了这个问题
options.SPOptions.Logger = new AspNetCoreLoggerAdapter(loggerFactory.CreateLogger<Saml2Handler>());

...当我设置选项时。

loggerFactory 指的是 Microsoft.Extensions.Logging.ILoggerFactory.

的注入实例