【问题标题】:How to set redirect_uri parameter on OpenIdConnectOptions for ASP.NET Core如何在 ASP.NET Core 的 OpenIdConnectOptions 上设置 redirect_uri 参数
【发布时间】:2016-06-28 20:40:45
【问题描述】:

我正在尝试使用 OpenId 将 ASP.NET 应用程序连接到 Salesforce,目前这是我的连接代码。我想我得到了除了 redirect_uri 参数之外的所有东西,它必须与另一端的值完全匹配。


 app.UseCookieAuthentication(x =>
        {
            x.AutomaticAuthenticate = true;
            x.CookieName = "MyApp";
            x.CookieSecure = CookieSecureOption.Always;
            x.AuthenticationScheme = "Cookies";
   
        });

        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();


        app.UseOpenIdConnectAuthentication(x =>
        {
            x.AutomaticAuthenticate = true;
            x.Authority = "https://login.salesforce.com";
            x.ClientId = "CLIENT_ID_HERE";
            x.ResponseType = "code";
            x.AuthenticationScheme = "oidc";
            x.CallbackPath = new PathString("/services/oauth2/success");
            //x.RedirectUri = "https://login.salesforce.com/services/oauth2/success";
            x.Scope.Add("openid");
            x.Scope.Add("profile");
            x.Scope.Add("email");                
        });

但是 RedirectUri 不是要传递的有效参数。正确的设置方法是什么?

【问题讨论】:

  • 对于 OIDC,添加 x.SignInScheme = "Cookies" 并删除 x.AutomaticAuthenticate

标签: asp.net-core openid-connect


【解决方案1】:

redirect_uri 是使用从当前请求中提取的方案、主机、端口和路径以及您指定的 CallbackPath 自动为您计算的。

x.RedirectUri = "https://login.salesforce.com/services/oauth2/success" 看起来非常可疑(除非您为 Salesforce 工作):不要忘记它是身份验证流程完成时用户代理将被重定向到的回调 URL,而不是身份提供者的授权端点。

因此,在您的情况下,用户将被重定向到 http(s)://yourdomain.com/services/oauth2/success。是您在 Salesforce 选项中注册的地址吗?

【讨论】:

  • 非常适合反向代理!
  • 如果自动计算redirect_uri,您如何处理在https://some.friendly.url.com 提供服务但在https://some-ugly-hidden-url.not.to.be.used 托管在Azure 上的应用程序?请看一下this好吗?
【解决方案2】:

你需要为OnRedirectToIdentityProvider设置一个事件监听

在你的情况下:

x.Events.OnRedirectToIdentityProvider = async n =>
{
    n.ProtocolMessage.RedirectUri = <Redirect URI string>;
    await Task.FromResult(0);
}

【讨论】:

  • 在生产过程中总是需要一些手动工作。至少他们以事件的形式做了扩展点。
  • 伟大的解决方案,今天节省了一天。谢谢!
  • 仅供参考,在 .NET 4.6 和所有版本的 .NET Core 中,您可以使用 Task.CompletedTask 而不是创建新的。
猜你喜欢
  • 2021-06-10
  • 1970-01-01
  • 2018-10-19
  • 2018-01-15
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
相关资源
最近更新 更多