asp.net-core - ASP .NET Core https 重写似乎不起作用

标签 asp.net-core asp.net-core-2.0

环境: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/

相关文章:

c# - Json.Net 和 Web API 中枚举的验证

asp.net-identity - 如何从 ASP.NET Identity Core 2.0 中删除与角色相关的表

asp.net-core - 访问被拒绝的查询字符串太长

ubuntu - Visual Studio Code Ubuntu aspnet core 2 容器调试

asp.net-core - 使用 asp.net core MVC 执行任何操作都会出现 404 错误

asp.net-core - Windows加密异常: Keyset does not exist

sql-server - 具有 Azure SQL 托管标识的 Entity Framework Core 导致用户“NT AUTHORITY\ANONYMOUS LOGON 登录失败”

asp.net - 使用 Azure AD 身份验证进行自定义授权

c# - 在 ASP.NET Core 的 Startup 类中访问服务

c# - DynamoDb .AsyncSearch<T>.GetRemainingAsync 随机挂起