public void ConfigureServices(IServiceCollection services)
{
// Irrelevant code removed
services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = StatusCodes.Status301MovedPermanently;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Irrelevant code removed
app.UseHttpsRedirection();
app.UseRewriter(new RewriteOptions()
.AddRedirectToWwwPermanent()
.AddRedirectToHttpsPermanent()
);
}
据我所知,我必须使用重写器来设置AddRedirectToWwwPermanent.我的问题是,我应该同时使用app.UseHttpsRedirection()和AddRedirectToHttpsPermanent()吗?或者,如果他们做同样的事情,我应该删除哪些?
我只是想确保我正确地重定向到Https,结合Wwww重定向.
AddRedirectToHttpsPermanent(或其兄弟
AddRedirectToHttps)将
RedirectToHttpsRule添加到重写器.这条规则
works like this:
if (!context.HttpContext.Request.IsHttps)
{
var host = context.HttpContext.Request.Host;
if (SSLPort.HasValue && SSLPort.Value > 0)
{
// a specific SSL port is specified
host = new HostString(host.Host, SSLPort.Value);
}
else
{
// clear the port
host = new HostString(host.Host);
}
var req = context.HttpContext.Request;
var newUrl = new StringBuilder().Append("https://").Append(host).Append(req.PathBase).Append(req.Path).Append(req.QueryString);
var response = context.HttpContext.Response;
response.StatusCode = StatusCode;
response.Headers[HeaderNames.Location] = newUrl.ToString();
context.Result = RuleResult.EndResponse;
context.Logger?.RedirectedToHttps();
}
因此,这基本上获取当前主机名,并构建一个看起来相同的新URL,除了它前面有一个https://.然后它设置301 HTTP状态代码并通过Location标头返回新的URL.
然后该规则作为RewriteMiddleware的一部分执行,它基本上只循环遍历所有已注册的规则,并最终运行上述代码,然后结束响应.
相比之下,这就是HttpsRedirectionMiddleware works internally:
if (context.Request.IsHttps || !TryGetHttpsPort(out var port))
{
return _next(context);
}
var host = context.Request.Host;
if (port != 443)
{
host = new HostString(host.Host, port);
}
else
{
host = new HostString(host.Host);
}
var request = context.Request;
var redirectUrl = UriHelper.BuildAbsolute(
"https",
host,
request.PathBase,
request.Path,
request.QueryString);
context.Response.StatusCode = _statusCode;
context.Response.Headers[HeaderNames.Location] = redirectUrl;
_logger.RedirectingToHttps(redirectUrl);
return Task.CompletedTask;
因此,这将从传入请求中获取主机名,然后使用UriHelper构建一个绝对URL,该URL看起来与当前请求完全相同,只是它使用https://方案.然后它设置307 HTTP状态代码结果,并通过Location标头返回新的URL.由于它不会调用以后的中间件,因此也会结束响应.
所以,是的,这两个解决方案是非常不同的(不是):它们使用几乎相同的代码并产生相同的结果.唯一的实际区别是HttpsRedirectionMiddleware默认使用HTTP 307 status code.
如果您更喜欢一个状态代码而不是另一个,则可以完全配置两个中间件以使用您首选的状态代码.
那么您应该使用哪个中间件来启用HTTPS重定向?这并不重要.默认情况下,ASP.NET Core模板附带HttpsRedirectionMiddleware,但该中间件也仅在ASP.NET Core 2.1之后才存在.
我个人坚持使用HttpsRedirectionMiddleware,因为它非常清楚地表达了它的目的.但是如果你有一个RewriteMiddleware,我只需用RedirectToHttpsRule替换HttpsRedirectionMiddleware来重写中间件,所以你只需要一个中间件来执行重定向. – 但最后,它确实没关系.
