【问题标题】:Can I change Identity Providers with OWIN and OpenID at run time?我可以在运行时使用 OWIN 和 OpenID 更改身份提供者吗?
【发布时间】:2022-01-28 06:39:42
【问题描述】:

我正在使用 OWIN 中间件来配置 OpenID 身份验证。此配置在 StartUp.cs 处调用,指向 B2C IDP。

public void ConfigureAuth(IAppBuilder app)
{
    // Required for Azure webapps, as by default they force TLS 1.2 and this project attempts 1.0
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        // ASP.NET web host compatible cookie manager
        CookieManager = new SystemWebChunkingCookieManager()
    });

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            // Generate the metadata address using the tenant and policy information
            MetadataAddress = String.Format(Globals.WellKnownMetadata, Globals.Tenant, Globals.DefaultPolicy),

            // These are standard OpenID Connect parameters, with values pulled from web.config
            ClientId = Globals.ClientId,
            RedirectUri = Globals.RedirectUri,
            PostLogoutRedirectUri = Globals.RedirectUri,

            // Specify the callbacks for each type of notifications
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                RedirectToIdentityProvider = OnRedirectToIdentityProvider,
                AuthorizationCodeReceived = OnAuthorizationCodeReceived,
                AuthenticationFailed = OnAuthenticationFailed,
            },

            // Specify the claim type that specifies the Name property.
            TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = "name",
                ValidateIssuer = false
            },

            // Specify the scope by appending all of the scopes requested into one string (separated by a blank space)
            Scope = $"openid profile offline_access {Globals.ReadTasksScope} {Globals.WriteTasksScope}",

            // ASP.NET web host compatible cookie manager
            CookieManager = new SystemWebCookieManager()
        }
    );
}

如何让中间件使用不同的配置,特别是对象OpenIdConnectAuthenticationOptions,以便在运行时指向不同的 IDP?

【问题讨论】:

    标签: model-view-controller owin openid-connect


    【解决方案1】:

    您可以注册多个命名的 openIDCConnect 处理程序,如

    .AddOpenIdConnect("Auth0", options =>
    {  Options...
    }
    .AddOpenIdConnect("google", options =>
    {  Options...
    }
    .AddOpenIdConnect("facebook", options =>
    {  Options...
    }
    

    然后用户可以选择他想要的身份验证方式,使用以下之一:

    HttpContext.SignInAsync("Auth0",....);
    HttpContext.SignInAsync("google",....);
    HttpContext.SignInAsync("facebook",....);
        
    

    当你添加多个handler时,你需要确保每个handler在客户端的本地回调路径是不同的,比如

    CallbackPath = new PathString("/signin-auth0");
    CallbackPath = new PathString("/signin-google");
    CallbackPath = new PathString("/signin-facebook");
    

    (您在选项中设置)

    【讨论】:

    • 我将接口Owin.IAppBuilder 对象传递给ConfigureAuth,它没有AddOpenIdConnect() 的功能。我会看看Microsoft.Extensions.DependencyInjection 看看这个AddOpenIdConnect 可以如何帮助我
    • 这个解决方案适用于 asp.net core 吗?还是MVC?看起来它适用于核心 - docs.microsoft.com/en-us/dotnet/api/…
    • 我的代码是为 ASP.NET Core 编写的,但你可以找到这里为旧版 ASP.NET stackoverflow.com/questions/43858586/…提供的相同想法
    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2018-08-30
    • 2020-08-28
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多