【问题标题】:ITfoxtec.Identity.Saml2 - multiple authentication schemasITfoxtec.Identity.Saml2 - 多种身份验证模式
【发布时间】:2021-01-14 13:05:25
【问题描述】:

有没有办法将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);
                    });

请指教。

【问题讨论】:

    标签: itfoxtec-identity-saml2


    【解决方案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。

    【讨论】:

      【解决方案2】:

      在这种情况下,您不能使用 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);
      

      【讨论】:

      • 这就是我的怀疑。会检查的。谢谢
      猜你喜欢
      • 1970-01-01
      • 2018-01-28
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 2021-11-27
      • 1970-01-01
      • 2010-12-31
      相关资源
      最近更新 更多