首页 > 解决方案 > 具有多个 OIDC 身份验证配置的 OWIN

问题描述

我的应用程序是 Asp.Net 4.7.2 Framework MVC。我想注册三个 OWIN OIDC Auth 配置并能够从中进行选择。

OpenIdConnectAuthenticationOptions oidcOptions1 = new OpenIdConnectAuthenticationOptions
            {
                ClientId = _oktaMvcOptions.ClientId,
                ClientSecret = _oktaMvcOptions.ClientSecret,
                Authority = _issuer,
                RedirectUri = _oktaMvcOptions.RedirectUri,
                ResponseType = OpenIdConnectResponseType.Code,
                RedeemCode = true,
                Scope = scopeString,
                PostLogoutRedirectUri = _oktaMvcOptions.PostLogoutRedirectUri,
                TokenValidationParameters = tokenValidationParameters,
                SecurityTokenValidator = new StrictSecurityTokenValidator(),
                AuthenticationMode = (_oktaMvcOptions.LoginMode == LoginMode.SelfHosted) ? AuthenticationMode.Passive : AuthenticationMode.Active,
                SaveTokens = true,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    RedirectToIdentityProvider = BeforeRedirectToIdentityProviderAsync,
                    SecurityTokenValidated = SecurityTokenValidatedAsync,
                    AuthenticationFailed = _oktaMvcOptions.AuthenticationFailed,
                },
            };

OpenIdConnectAuthenticationOptions oidcOptions2 = new OpenIdConnectAuthenticationOptions{...};

OpenIdConnectAuthenticationOptions oidcOptions3 = new OpenIdConnectAuthenticationOptions{...};

在 Startup.cs 中,配置如下所示:

public void Configuration(IAppBuilder app)
{
app.UseOpenIdConnectAuthentication(oidcOptions1);   
app.UseOpenIdConnectAuthentication(oidcOptions2);   
app.UseOpenIdConnectAuthentication(oidcOptions3);
}

它们都是 OpenIdConnect 类型。当我打电话给挑战时:

HttpContext.GetOwinContext().Authentication.Challenge();

我如何告诉挑战使用 oidcOptions2 或 oidcOptions3?如何指定使用哪一个?

谢谢。

标签: c#authenticationmodel-view-controllerowinopenid-connect

解决方案


没关系 - 很容易:

OpenIdConnectAuthenticationOptions oidcOptions1 = new OpenIdConnectAuthenticationOptions("first")
{
...
}

然后:

HttpContext.GetOwinContext().Authentication.Challenge("first");

推荐阅读