【问题标题】:Setting redirect_uri on AspNetCore OpenIdConnect在 AspNetCore OpenIdConnect 上设置 redirect_uri
【发布时间】:2018-10-19 17:45:45
【问题描述】:

在 .Net 上,当我创建 Open ID 连接身份验证选项时,我有一个属性来设置 RedirectUri,这甚至按照文档中的建议进行了定义,但 AspNetCore 上不存在此类属性,它会自动设置为当前服务器 EX :(http://localhost),有办法改变吗?

试图为此寻找解决方案我遇到了新 AspNetCore 身份验证的许多缺点,这是生产就绪还是只是 WIP?

【问题讨论】:

  • 您使用的是什么版本的 asp.net core?在您要设置选项的位置显示您的代码。从 asp.net 核心版本 1.x 到 2.x 发生了很大变化。我认为 OpenIdConnectOptions 上的属性是 CalbackPath
  • @JoeAudette 版本是2.x,并且没有属性不是响应重定向后附加到responseURI的CallbackPath。

标签: c# asp.net-core


【解决方案1】:

在摆弄这个之后,我发现你必须为OnRedirectToIdentityProvider 事件设置一个事件监听器。

services.AddOpenIdConnect(options =>
{
    Configuration.Bind("<Json Config Filter>", options);
    options.Events.OnRedirectToIdentityProvider = async context =>
    {
        context.ProtocolMessage.RedirectUri = "<Return URI String>";
        await Task.FromResult(0);
    };
});

【讨论】:

  • 您不需要设置它。当前值有什么问题?
  • 在我的具体情况下,我将其设置为 null,因为我正在生成从开发机器到仅回复客户端特定 URI 的客户端服务器的请求,然后我得到响应并检查响应格式良好,在配置良好的环境中,这绝不是必要的,但就我而言,我无法访问客户端配置。
  • 这是我的用例。如果我的应用程序托管在处理 HTTPS 的平台上,但我有一个 F5 设备在请求到达应用程序之前卸载 SSL 证书,那么应用程序在技术上解析为 HTTP,但浏览器说的是 HTTPS。然后中间件检测 HTTP 并以“redirect_uri=http://”重定向到 oidc。 OIDC 看到它,重定向到 HTTP,设置了 cookie,我的应用程序重定向到 HTTPS,因为 F5 不允许 HTTP,HTTPS 命中,cookie 未设置,因为它设置为 HTTP 而不是 HTTPS,重定向回 OIDC。无限重定向循环。
  • 这很有帮助,但使用它不会让我登录到中间件。所以我一直在尝试登录,然后来回走动。我已经删除了回调:auth0.com/docs/quickstart/backend/aspnet-core-webapi/…,现在对context.ProtocolMessage.RedirectUri = config.Callback; 有什么想法吗?
【解决方案2】:

我正在更改架构如下

public static void AddCookieAuthentication(this IServiceCollection services, IConfiguration configuration)
    {
        _configuration = configuration;
        services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
                options.TokenValidationParameters.AuthenticationType = IdentityConstants.ApplicationScheme;
                options.ResponseType = "code";
                options.MetadataAddress = configuration["Authentication:Cognito:MetadataAddress"];
                options.ClientId = configuration["Authentication:Cognito:ClientId"];
                options.ClientSecret = configuration["Authentication:Cognito:ClientSecret"];
                options.GetClaimsFromUserInfoEndpoint = true;
                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.Scope.Add("email");
                options.Scope.Add("aws.cognito.signin.user.admin");

                options.Events = new OpenIdConnectEvents
                {
                    // this makes signout working
                    OnRedirectToIdentityProviderForSignOut = OnRedirectToIdentityProviderForSignOut,
                    OnRedirectToIdentityProvider = OnRedirectToIdentityProvider,
                };
            });
    }

    private static Task OnRedirectToIdentityProvider(RedirectContext ctx)
    {
        ctx.Options.Events.OnRedirectToIdentityProvider = async context =>
        {
            **context.ProtocolMessage.RedirectUri = context.ProtocolMessage.RedirectUri.Replace("http:", "https:");**
            await Task.FromResult(0);
        };
        return Task.CompletedTask;
    }

【讨论】:

    猜你喜欢
    • 2016-11-07
    • 2021-03-02
    • 2016-09-20
    • 1970-01-01
    • 2017-09-29
    • 2016-06-28
    • 2018-09-30
    • 2016-11-06
    • 2021-10-07
    相关资源
    最近更新 更多