【问题标题】:ASP.NET Core redirect http to httpsASP.NET Core 将 http 重定向到 https
【发布时间】:2017-09-05 07:24:20
【问题描述】:

我在 web.config 中创建了一个重定向规则,将我的网站从 http 重定向到 https。我遇到的问题是网站上的每个链接现在都是 https。我有很多指向其他没有 SSL 的网站的链接,因此我收到证书错误。这就是我所做的:

  <rewrite>
  <rules>
    <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

我怎样才能只为我的域而不是我网站上的每个链接重定向 https?

【问题讨论】:

标签: asp.net asp.net-mvc ssl redirect asp.net-core


【解决方案1】:

在 ASP.NET Core 2.2 中,您应该使用 Startup.cs 设置将 http 重定向到 https

所以在 ConfigureServices 中添加这个:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpsRedirection(options =>
    {
        options.HttpsPort = 443;
    });                           // <=== Add this (AddHttpsRedirection) =====

    services.AddRazorPages();  
}

并在配置中添加:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();            // <=== Add this =====
    }

    app.UseHttpsRedirection();    // <=== Add this =====
}

launchSettings.json 中检查是否存在打击项目:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:63278",
      "sslPort": 44377      // <<===  check this port exists.
    }
  },
  "YOUR_PROJECT_NAME": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",  // <=== check https url exists.
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

那就尽情享受吧。

【讨论】:

    【解决方案2】:

    编辑:虽然它仍然有效,但自从我写下这个答案以来,情况发生了一些变化。请查看 D.L.MAN 的更多最新答案:https://stackoverflow.com/a/56800707/844207

    在 asp.net Core 2 中,您可以使用独立于 Web 服务器的 URL 重写,通过使用 App.UseRewriter 在 Startup.Configure 中,像这样:

            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
    
                // todo: replace with app.UseHsts(); once the feature will be stable
                app.UseRewriter(new RewriteOptions().AddRedirectToHttps(StatusCodes.Status301MovedPermanently, 443));
            }
    

    【讨论】:

    • UseRewriter 方法来自以下using语句:using Microsoft.AspNetCore.Rewrite;
    【解决方案3】:

    您还需要在 .net core 2.1 中添加以下代码

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }
    
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
    
        app.UseMvc();
    }
    

    以及服务配置中的以下部分

           services.AddHttpsRedirection(options =>
           {
            options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
            options.HttpsPort = 5001;
            });
    

    【讨论】:

      【解决方案4】:

      asp.net core

              // IHostingEnvironment (stored in _env) is injected into the Startup class.
              if (!_hostingEnvironment.IsDevelopment())
              {
                  services.Configure<MvcOptions>(options =>
                  {
                      options.Filters.Add(new RequireHttpsAttribute());
                  });
              }
      

      【讨论】:

        【解决方案5】:

        在 ASP.NET Core 2.1 中只需使用这个:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();   // <-- Add this !!!!!
            }
        
            app.UseHttpsRedirection(); // <-- Add this !!!!!
            app.UseStaticFiles();
            app.UseCookiePolicy();
        
            app.UseMvc();
        }
        

        【讨论】:

        【解决方案6】:

        实际上 (ASP.NET Core 1.1) 有一个名为 Rewrite 的 middleware,其中包含您要执行的操作的规则。

        你可以像这样在 Startup.cs 上使用它:

        var options = new RewriteOptions()
            .AddRedirectToHttpsPermanent();
        
        app.UseRewriter(options);
        

        【讨论】:

        猜你喜欢
        • 2020-04-21
        • 2017-08-13
        • 1970-01-01
        • 2011-09-28
        • 1970-01-01
        • 2013-03-20
        • 2011-07-15
        • 2017-11-13
        • 1970-01-01
        相关资源
        最近更新 更多