如何在ASP.NET Core的OpenIdConnectOptions上设置redirect_uri参数

Val*_*ion 8 openid-connect asp.net-core

我正在尝试使用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");                
        });
Run Code Online (Sandbox Code Playgroud)

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

Kév*_*let 11

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.friend.url.com”上提供服务但托管在Azure上“https://some-ugly-hidden-url.not”上的应用程序.待.使用`?您能看一下[这个](/sf/ask/4695880311/)吗? (2认同)

Ped*_*Kid 7

您需要设置一个事件来监听 OnRedirectToIdentityProvider

在您的情况下:

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

  • 仅供参考,在 .NET 4.6 和所有版本的 .NET Core 中,您可以使用“Task.CompletedTask”而不是创建新任务。 (3认同)