相关问题
- Using IApplicationBuilder.Map generates nested paths with UseMvc
- CallbackPath implementation
- Redirect URI with Google using asp.net MVC
- How to configure ASP.net Core server routing for multiple SPAs hosted with SpaServices
问题
我有一个服务在域的特定路径下运行,例如https://www.example.com/myservice . myservice 路径专用于我的服务,其他服务在同一域中有其他路径。在启动配置中是这样设置的:
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))}";
}
登录 Controller
这是用户可以提供凭据并验证它们的地方。验证通过后,用户被重定向到回调路径。
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
以便我的应用程序可以在不添加重复前缀的情况下处理它?</p>
最佳答案
MapMiddleware
正在将匹配的路径添加到 Request.PathBase
,因此您可以在创建返回 url 时使用它
string returnUrl = Context.Request.PathBase + Options.CallbackPath;
关于c# - 使用 RemoteAuthenticationHandler CallbackPath 与 IApplicationBuilder 路径匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249177/