【问题标题】:Using RemoteAuthenticationHandler CallbackPath with IApplicationBuilder path match将 RemoteAuthenticationHandler CallbackPath 与 IApplicationBuilder 路径匹配一起使用
【发布时间】:2019-10-05 14:18:20
【问题描述】:

相关问题

问题

我有一个服务在域的特定路径下运行,例如https://www.example.com/myservicemyservice 路径专用于我的服务,其他服务在同一域中具有其他路径。在启动配置中是这样设置的:

app.Map("/myservice", builder =>
{
    builder.UseStaticFiles();
    builder.UseMvcWithDefaultRoute();
});

我正在使用一个实现自定义RemoteAuthenticationHandler 的库。默认情况下,回调路径路由到/x-callback,这会导致浏览器尝试访问https://www.example.com/x-callback

由于我的服务不处理没有 /myservice 前缀的 url,我得到一个 404。将浏览器中的 URL 更改为 /myservice/x-callback 手动加载回调,一切都很好。

我可以在启动配置服务中按预期在启动选项中设置处理程序的回调路径。

services.AddSomething(options =>
{
    options.AddThingX((o) =>
    {
        o.CallbackPath = new PathString($"/myservice{o.CallbackPath}");
    });
});

当我设置回调路径时,浏览器会尝试加载/myservice/x-callback。但是,这个 URL 现在返回一个 404。回调的处理程序似乎也更改了其 URL。将浏览器中的 URL 更改为 /myservice/myservice/x-callback 会按预期加载回调。

RemoteAuthenticationHandler

这是处理程序中处理质询并使用回调路径的代码。它将回调路径设置为登录 url 的查询参数。

protected override Task HandleChallengeAsync(AuthenticationProperties properties)
{
    // Add options etc
    // ...
    // ...

    // This defines the login url, with a query parameter for the CallbackPath
    var loginUrl = GetLoginUrl(loginOptions);
    Response.Redirect(loginUrl);

    return Task.CompletedTask;
}

private string GetLoginUrl(MyServiceLoginOptions loginOptions)
{
    // This is where the return url is set. The return url
    // is used after login credentials are verified.
    return $"{Options.LoginPath}" +
            $"?returnUrl={UrlEncoder.Encode(Options.CallbackPath)}" +
            $"&loginOptions={UrlEncoder.Encode(_loginOptionsProtector.Protect(loginOptions))}";
}

登录控制器

用户可以在此处提供凭据并对其进行验证。验证后将用户重定向到回调路径。

private async Task<ActionResult> ChallengeComplete(LoginStatusRequest request, ChallengeResponse challengeResponse)
{
    // auth logic
    // ...
    // All is fine, the users credentials have been verified. Now
    // we can redirect to the CallbackPath.
    return Ok(Response.Finished(returnUri));
}

注意 我可以重写 URL,但如果可能的话,我想使用“正确的”/myservice 路径以避免混淆并可能导致其他服务出现问题(尽管可能性很小)。

问题

如何在回调路径前加上 /myservice 前缀,以便我的应用程序可以在不添加重复前缀的情况下处理它?

【问题讨论】:

  • UseAuthentication 的电话在哪里? Map里面没有显示,所以是在根IApplicationBuilder里面吗?
  • @KirkLarkin 我正在使用 IdentityServer,它在 Configure 内部,如您所料:app.UseIdentityServer();
  • 无视问题并寻求解决方案:o.CallbackPath = new PathString(o.CallBackPath); 会起作用吗?
  • @LosManos 不错的尝试!不幸的是没有工作:P。它重定向到/x-callback 而不是/myservice/x-callback
  • 那是new PathString骗了你吗?

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

MapMiddleware是将匹配的路径添加到Request.PathBase,所以你可以在创建返回url时使用它

string returnUrl = Context.Request.PathBase + Options.CallbackPath;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多