首页 > 解决方案 > 带有 IAuthenticationSchemeProvider 的 IdentityServer4 的动态方案 SAML 2.0

问题描述

我正在尝试将 IdentityServer4 设置为与多个外部 IdP 一起使用,这些 IdP 是在IAuthenticationSchemeProvider'sAddScheme方法的帮助下动态添加的。

我已经成功地为OpenIdConnectIdP 完成了它,但是在使用Saml2p基于 IdP 时遇到了一些麻烦。在这个示例之后,我对 Saml2p 遵循了相同的逻辑:

注入IOptionsMonitorCache<Saml2pAuthenticationOptions>和:

if (await _schemeProvider.GetSchemeAsync(scheme) == null)
            {
                _schemeProvider.AddScheme(new AuthenticationScheme(scheme, scheme, typeof(Saml2pAuthenticationHandler)));
            }
            else
            {
                 _saml2pOptionsCache.TryRemove(scheme);
            }
            _saml2pOptionsCache.TryAdd(scheme, samlOptions);

我遇到了一个例外:

Unable to resolve service for type 'Rsk.AspNetCore.Authentication.Saml2p.Factories.ISamlFactory``1[IdentityServer4.Saml.Generators.Interfaces.IServiceProviderMetadataGenerator]' while attempting to activate 'Rsk.AspNetCore.Authentication.Saml2p.Saml2pAuthenticationHandler'.

我不确定在添加方案时是否应该设置一些关于 Saml 的额外配置,任何帮助表示赞赏。

编辑:我将 Rsk NuGet 用于 SAML 2.0

标签: authenticationidentityserver4saml-2.0

解决方案


AddSaml2p注册依赖加载以及身份验证处理程序的调用。

我要么AddSaml2p在你的代码中调用某个地方,要么自己注册所需的依赖项,如下所示:

builder.Services.AddMemoryCache();
builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

builder.Services.TryAddScoped<ISamlFactory<IServiceProviderMetadataGenerator>, 
builder.Services.TryAddScoped<ISamlFactory<ISaml2SingleSignOnRequestGenerator>, Saml2SingleSignOnRequestGeneratorFactory>();
builder.Services.TryAddScoped<ISamlFactory<ISaml2SingleLogoutRequestGenerator>, Saml2SingleLogoutRequestGeneratorFactory>();
builder.Services.TryAddScoped<ISamlFactory<ISaml2SingleSignOnResponseValidator>, Saml2SingleSignOnResponseValidatorFactory>();

builder.Services.TryAddScoped<ISamlBindingService, SamlBindingService>();
builder.Services.TryAddScoped<ISamlSigningService, SamlSigningService>();
builder.Services.TryAddScoped<IDateTimeService, SystemClockDateTimeService>();
builder.Services.TryAddScoped<ISamlTimeComparer, SamlTimeComparer>();
builder.Services.TryAddScoped<ISamlCorrelationStore, CookieCorrelationStore>();

推荐阅读