ITfoxtec.Identity.Saml2 - 多个身份验证模式

ITfoxtec.Identity.Saml2 - multiple authentication schemas

有没有办法将SAML认证和表单认证集成到同一个项目中?

我今天只有 SAML 身份验证:

 services.AddSaml2("/login", true);

如果我在 SAML 之后添加另一个架构,SAML 将停止工作。如果我之前添加它,则不会触发 from 身份验证。 这是表单验证的代码:

services.AddAuthentication("Form")
                    .AddScheme<FormAuthenticationOptions, FormAuthenticationHandler>("Form", null)
                    .AddCookie(options => {
                        options.LoginPath = "....";
                        options.LogoutPath = "...";
                        options.EventsType = typeof(CustomCookieAuthenticationEvents);
                    });

请指教

在这种情况下您不能使用 services.AddSaml2,因为该方法不 return AuthenticationBuilder。

https://github.com/ITfoxtec/ITfoxtec.Identity.Saml2/blob/master/src/ITfoxtec.Identity.Saml2.MvcCore/Configuration/Saml2ServiceCollectionExtensions.cs#L15

相反,您必须将方法中的代码与新的身份验证模式结合使用。

可能是这样的,不过我没试过:

services.AddAuthentication(Saml2Constants.AuthenticationScheme)
    .AddCookie(Saml2Constants.AuthenticationScheme, o =>
    {
        o.LoginPath = new PathString(loginPath);
        o.SlidingExpiration = slidingExpiration;
        if(!string.IsNullOrEmpty(accessDeniedPath))
        {
            o.AccessDeniedPath = new PathString(accessDeniedPath);
        }
    })
    .AddScheme<FormAuthenticationOptions, FormAuthenticationHandler>("Form", null);

我检查了它并使其仅按如下方式工作:

// Add SAML2 schema 
                services.AddAuthentication(Saml2Constants.AuthenticationScheme)
                    .AddCookie(Saml2Constants.AuthenticationScheme, o => {
                            o.LoginPath = new PathString("loginPath");
                            o.SlidingExpiration = true;
                        }
                    );

 services.AddAuthentication("TMP")
                    .AddPolicyScheme("TMP", "TMP Authorization", options => {
                        options.ForwardDefaultSelector = context => {
                            if (context.Request.Headers["Form"].Any() || context.Request.Cookies.ContainsKey("Form")) {
                                return FormAuthenticationOptions.Schema;
                            }
                            return Saml2Constants.AuthenticationScheme;
                        };
                    })
                    .AddScheme<FormAuthenticationOptions, FormAuthenticationHandler>("Form", null)
                    .AddCookie(options => {
                        options.LoginPath = LoginPath ;
                        options.LogoutPath = LogoutPath ;
                        options.EventsType = typeof(CustomCookieAuthenticationEvents);
                    });

itfoxtec 将其模式添加为默认模式的原因。因此,我添加了我的模式策略,并通过添加 HTTP header 和 cookie 来决定要使用的模式。

不是很优雅,但是很管用。 我认为你可以通过像这样添加它来启用只添加你的库会很好

 .AddScheme<SamlAuthenticationOptions, SamlAuthenticationHandler>(FormAuthenticationOptions.Schema, null)

并将身份验证逻辑移至 SamlAuthenticationHandler。