我有一个中间件类,它读取用户的cookie并验证它们,然后在它们无效或丢失时重定向到登录URL。 用户重定向到的 URL 包含"back"参数。 此中间件在静态文件 SPA 上以及当应用程序不是 IIS 中的嵌套应用程序时,可以正常工作。
我的问题,这在使用 MVC 控制器和视图时不起作用,在用户被重定向到登录页面之前,路径(指向嵌套的 IIS 站点(被剥离,并且后面的 URL 不包含 URL 路径。 有什么想法吗?
会发生什么
转到 ->http://test.com/mysite
重定向至 ->http://login.com?back=http://test.com
应该发生什么
转到 ->http://test.com/mysite
重定向至 ->http://login.com?back=http://test.com/mysite
public class MyMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private readonly IOptions<MyMiddlewareOptions> _options;
public MyMiddleware(RequestDelegate next, IOptions<MyMiddlewareOptions> options, ILoggerFactory logger)
{
_next = next;
_options = options;
_logger = logger.CreateLogger("My Middleware");
}
public async Task Invoke(HttpContext context)
{
var middlewareCookieValidator = context.RequestServices.GetService<IMiddlewareCookieValidator>();
await new CookieRequestCultureProvider().DetermineProviderCultureResult(context);
if (!_options.Value.Bypass && !Path.HasExtension(context.Request.Path.Value))
{
try
{
if (wslCookieValidator.HasCreatedCookies(context) || await middlewareCookieValidator.ValidateCookiesAsync())
{
context.Response.OnStarting(async () => await middlewareCookieValidator.GenerateAndApplyCookiesToResponse(context));
await _next(context);
}
else
{
var location = new Uri($"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}{context.Request.QueryString}");
context.Response.Redirect($"{_options.Value.Url}?back={location}");
}
}
catch (Exception exception)
{
throw new Exception($"RequestDelegate '_next' = {_next}. {exception.Message}");
}
}
else
{
await _next(context);
}
}
}
}
这可能是 URL 编码问题。应使用System.Net.WebUtility.UrlEncode()
对传递到back
查询字符串参数中的 URL 进行编码。例如:
using System.Net;
// ...
var location = new Uri($"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}{context.Request.QueryString}");
context.Response.Redirect($"{_options.Value.Url}?back={WebUtility.UrlEncode(location)}");
@Lex Li
感谢您的回答,原来是由于 IIS 与子应用程序一起运行。
我需要以下内容才能正常工作。
var location = new Uri($"{context.Request.Scheme}://{context.Request.Host}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}");