【问题标题】:multiple authentication providers owin拥有多个身份验证提供商
【发布时间】:2016-02-29 17:57:40
【问题描述】:

我正在尝试根据组织请求实施多重身份验证。我在 startup.auth.cs 中有如下内容

 foreach (OrganizationModel org in orgList)
    {
        if (org.AuthenticationType != "Azure")
        {
            var adfs = new WsFederationAuthenticationOptions
            {
                AuthenticationType = org.AuthenticationType,
                Caption = org.Caption,
                BackchannelCertificateValidator = null,
                MetadataAddress = org.MetadataUrl,
                Wtrealm = org.Realm,
                Notifications = new WsFederationAuthenticationNotifications
                {
                    AuthenticationFailed = context =>
                    {
                        context.HandleResponse();
                        context.Response.Redirect("Home/Error?message=" + context.Exception.Message);
                        return Task.FromResult(0);
                    }
                },
                TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false },

            };
            app.UseWsFederationAuthentication(adfs);
        }
        else
        {
            var azure = new WsFederationAuthenticationOptions
            {
                AuthenticationType = org.AuthenticationType,
                Caption = org.Caption,
                BackchannelCertificateValidator = null,
                MetadataAddress = org.MetadataUrl,
                Wtrealm = org.Realm,
                Notifications = new WsFederationAuthenticationNotifications
                {
                    AuthenticationFailed = context =>
                    {
                        context.HandleResponse();
                        context.Response.Redirect("Home/Error?message=" + context.Exception.Message);
                        return Task.FromResult(0);
                    }
                },
                TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false },

            };
            app.UseWsFederationAuthentication(azure);
        }
    }

我为登录填充了各种身份验证提供程序。当我单击 ADFS 能够进行身份验证、获取声明时,一切正常。但是当我尝试对 Azure AD 进行身份验证时,我收到错误“ID 4037”,无法解析验证签名所需的密钥。 注意:如果我尝试单独执行 Azure AD(评论 ADFS 部分),它可以正常工作。 Orglist 从数据库中填充,它包含元数据 url、领域等信息。出于开发目的,我已将 https://localhost:44303 配置为两者的领域。

我登录后的回调方法是

 [AllowAnonymous]
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return RedirectToAction("Login");
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
                case SignInStatus.Failure:
                default:
                    // If the user does not have an account, then prompt the user to create an account
                    ViewBag.ReturnUrl = returnUrl;
                    ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                    return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.DefaultUserName});
            }
        }

指导我哪里出错了

【问题讨论】:

    标签: azure authentication oauth owin adfs


    【解决方案1】:

    我发现了问题所在。当我们有多个身份验证提供程序时,添加到 OWIN 中间件管道的每个身份验证选项的身份验证类型应该是唯一的。 对于尝试实施类似解决方案的人,下面给出了对我有用的代码。

     foreach (OrganizationModel org in orgList)
                {
                    switch (org.AuthenticationName)
                    {
                        case "ADFS":
                                    var adfs = new WsFederationAuthenticationOptions
                                          {
                                              AuthenticationType = org.AuthenticationType,
                                              Caption = org.Caption,
                                              BackchannelCertificateValidator = null,
                                              MetadataAddress = org.MetadataUrl,
                                              Wtrealm = org.Realm,
                                              SignOutWreply = org.Realm,
                                              Notifications = new WsFederationAuthenticationNotifications
                                              {
                                                  AuthenticationFailed = context =>
                                                  {
                                                      context.HandleResponse();
                                                      context.Response.Redirect("Home/Error?message=" + context.Exception.Message);
                                                      return Task.FromResult(0);
                                                  }
                                              },
                                              TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false },
                                          };
                            app.UseWsFederationAuthentication(adfs);
                            break;
                        case "Azure":
                            OpenIdConnectAuthenticationOptions azure = null;
                            azure = new OpenIdConnectAuthenticationOptions
                            {
                                AuthenticationType = org.AuthenticationType,
                                Caption = org.Caption,
                                BackchannelCertificateValidator = null,
                                Authority = org.MetadataUrl,
                                ClientId = org.ClientId,
                                RedirectUri = org.Realm,
                          PostLogoutRedirectUri=org.Realm,
                                Notifications = new OpenIdConnectAuthenticationNotifications
                             {
                                 AuthenticationFailed = context =>
                                 {
                                     context.HandleResponse();
                                     context.Response.Redirect("Home/Error?message=" + context.Exception.Message);
                                     return Task.FromResult(0);
                                 }
                             },
                            };
                            app.UseOpenIdConnectAuthentication(azure);
                            break;
                        case "Shibboleth":
                        break;
                        default:
                            break;
                    }
                }
    

    【讨论】:

    • 我认为你在这里所做的只是解决这个问题。据我所知,当您有多个提供程序时会出现问题,其中两个或更多提供者正在使用 WS-Federation。在您的解决方案中,您刚刚将 Azure 更改为使用 OpenID Connect。它有效,但并不能真正解决最初的问题。
    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 2013-06-09
    • 2017-01-10
    • 2021-02-28
    • 2015-05-03
    相关资源
    最近更新 更多