情况草图: 我已经在asp.net核心创建了一个网站,我已经开始运行了. 几天前,我的托管服务提供商添加了配置SSL的选项,所以我这样做让我遇到了没有从http重定向到https的问题. 我已经开始
我已经在asp.net核心创建了一个网站,我已经开始运行了.
几天前,我的托管服务提供商添加了配置SSL的选项,所以我这样做让我遇到了没有从http重定向到https的问题.
我已经开始在网上寻找解决方案,并找出了一些代码,这些代码在主机上的4个案例中的3个中有效,在IIS上本地测试的4个案例中有4个.
什么有效:
> https://www.someurl.com – >不需要重定向
> https://someurl.com – >重定向到https://www.someurl.com
> http://someurl.com – >重定向到https://www.someurl.com
> http://www.someurl.com – >给出重定向错误:ERR_TOO_MANY_REDIRECTS
以下代码用于asp.net核心站点:
public void Configure(...) {
app.UseHttpsWorldWideWebRedirect();
}
public static class HttpsWorldWideWebRedirectMiddlewareExtensions
{
public static IApplicationBuilder UseHttpsWorldWideWebRedirect(this IApplicationBuilder builder)
{
return builder.UseMiddleware<HttpsWorldWideWebRedirectMiddleware>();
}
}
public class HttpsWorldWideWebRedirectMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<HttpsWorldWideWebRedirectMiddleware> _logger;
public HttpsWorldWideWebRedirectMiddleware(RequestDelegate next, ILogger<HttpsWorldWideWebRedirectMiddleware> logger)
{
_next = next;
_logger = logger;
_logger.LogInformation("Https WWW redirect module loaded");
}
public async Task Invoke(HttpContext context)
{
var protoHeader = context.Request.Headers["X-Forwarded-Proto"].ToString();
var ignoreHttps = false;
var ignoreWww = false;
if (context.Request.IsHttps || protoHeader.ToLower().Equals("https"))
{
ignoreHttps = true;
}
if (context.Request.Host.Host.StartsWith("www", StringComparison.OrdinalIgnoreCase) || string.Equals(context.Request.Host.Host, "localhost", StringComparison.OrdinalIgnoreCase))
{
ignoreWww = true;
}
if (ignoreWww && ignoreHttps)
{
await _next.Invoke(context);
}
else
{
var www = ignoreWww ? "" : "www.";
var newPath =
$"https://{www}{context.Request.Host.Value}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}";
_logger.LogDebug($"'{context.Request.GetDisplayUrl()}' was rewritten into '{newPath}'");
context.Response.Headers["X-Forwarded-Proto"] = "https";
context.Response.Redirect(newPath, true);
}
}
}
我尝试的下一步是在web.config中添加一个重定向,以涵盖使用以下代码无法运行asp.net重定向的情况:
<rewrite>
<rules>
<rule name="Redirect to https if http://www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^www.*"/>
<add input="{HTTPS}" pattern="Off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
作为测试我添加了“iwork”这一行:
结果仍然是错误的重定向太多,但随后将http://someurl.com/iworkiworkiworkiworkiworkiworkiworkiworkiworkiwork作为生成的url.
所以我对于究竟出了什么问题以及如何解决它的想法,任何想法都感到茫然?
对.NET Core一无所知,但这肯定可以通过URL重写规则来完成.您至少需要2个重写规则.
>使用www将http请求重定向到https.
>重定向https请求,没有www.使用www前缀到https.
这将尽可能减少重定向链.
<rewrite>
<rules>
<rule name="Redirect all http requests to https with www" stopProcessing="true">
<match url=".*" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^localhost" negate="true" />
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)$" />
</conditions>
<action type="Redirect" url="https://www.{C:1}/{R:0}" />
</rule>
<rule name="Redirect https requests with no www prefix to https with www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost" negate="true" />
<add input="{HTTPS}" pattern="on" />
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>
</rules>
</rewrite>
