繁体   English   中英

ASP.Net 核心 HTTP 请求永远不会到达我的 API,并且不会重定向到 HTTPS

[英]ASP.Net core HTTP requests never reach my API, and wont redirect to HTTPS

When I call my asp.net core API using an HTTP URL, the request never reaches my app, I've added a middleware to test this, and the request never hits the context. 虽然我已经配置了 https 重定向,如下所示,没有 Http 请求重定向到 https。 但是,当我使用 HTTPS URL 时,请求现在会到达我的应用程序。 我希望能够从 Http URL 调用我的 API 然后重定向到 Z5E056C5050A1C47BADEZ57D。

我已经尝试了我在互联网上看到的所有内容。 我尝试启用 HTTPS 重定向,但没有任何效果。 这是 https 重定向的样子:

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
    }
    else {
        app.ConfigureExceptionMiddleware();
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseMvc();
}

在 configureServices 方法中:

services.AddHttpsRedirection(options => {
    options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
    options.HttpsPort = 44382;
});

启动设置.json

 "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:53471",
      "sslPort": 44382
    }
  },

该代码不起作用并且不启用重定向。 它只允许我通过 HTTPS 访问我的应用程序,但未完成重定向。 谁能告诉我如何调用 Http 重定向到 https?

导致此问题的原因是我的启动设置中有两个配置文件。json 并且我选择了错误的配置文件来启动应用程序。 选择具有属性的配置文件(在项目设置中):“commandName”:“Project”解决了这个问题。 对我的回答发表评论的人引导我走上解决这个问题的正确轨道。

我认为您缺少 HTTPS 端口,因此您的应用程序应该记录错误消息"Failed to determine the https port for redirect."

您可以使用以下方法之一配置端口。

  • 设置HttpsRedirectionOptions.HttpsPort
  • 设置ASPNETCORE_HTTPS_PORT环境变量(重新启动计算机以反映更改)
  • 您可以在 appsettings.json 文件中配置https_port端口,例如:

     { "https_port": 443 }
  • 在 launchsettings.json 中设置 HTTPS URL

我希望其中一种方法可以解决您的问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM