1

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

我今天只有 SAML 身份验证:

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

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

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

请指教。

4

2 回答 2

1

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

// 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 标头和 cookie 来决定要使用的架构。

不是那么优雅,但有效。我认为你会很好地通过像这样添加它来添加你的库

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

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

于 2021-01-17T15:53:36.047 回答
0

在这种情况下,您不能使用 services.AddSaml2,因为该方法不返回 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);
于 2021-01-15T08:12:24.150 回答