环境:ASP.NET Core 2.0。应用程序部署到 Windows Server 2016 上的 IIS。
在我的startup.cs中,我有以下代码:
public void ConfigureServices(IServiceCollection services)
...
services.Configure<MvcOptions>(options => {
options.Filters.Add(new RequireHttpsAttribute());
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
...
var options = new RewriteOptions().AddRedirectToHttps();
app.UseRewriter(options);
}
但是,当应用程序部署到 IIS 时,我看不到 <a href="http://xxx" rel="noreferrer noopener nofollow">http://xxx</a>
更改为 <a href="https://xxx" rel="noreferrer noopener nofollow">https://xxx</a>
在浏览器中。
我可以直接去<a href="https://xxx" rel="noreferrer noopener nofollow">https://xxx</a>
这似乎有效。然而,其想法是自动将 http 请求重定向到 https。
我错过了什么?我还需要在 IIS 上做一些事情吗?问候。
最佳答案
从您发布的代码片段来看,app.UseRewriter(options);
是 Configure()
方法中的最后一个。将 app.UseMvc();
移动到方法的最底部,因为管道中的排序顺序非常重要。我怀疑 MVC 在实际应用重写规则之前处理您的请求。
另外:
- 您可能不需要
RequireHttpsAttribute
- 在没有过滤器的情况下运行代码,HTTPS 重定向仍然应该发生。 - 这可能取决于您的要求,但我通常将 301(永久)重定向设置为 HTTPS。有一个针对
AddRedirectToHttpsPermanent()
的扩展。
关于asp.net-core - ASP .NET Core https 重写似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47701807/