0

I have a clean .net 5 api project that will always redirect(307) to https for some reason when accessing the site thew iis 10. The site didn't have https enabled when it was created. The only thing special about the setup is that the site in iis uses a custom domain (api.projectname.dev) defined in the host file that points to 127.0.0.1. The the api doesn't redirect if I run it using iis express using visual studio. And the default iis site doesn't redirect to https. Does anyone know what might be the cause of this?

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "project.Api", Version = "v1" });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "project.Api v1"));
        }

        app.UseRouting();

        //app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="bin\Debug\net5.0\project.Api.exe" arguments="" stdoutLogEnabled="false" hostingModel="InProcess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
studiobram
  • 41
  • 5
  • Does your startup contains `app.UseHsts();`? – Magnetron Feb 25 '21 at 18:28
  • Do search on Google for "http force to https" – jdweng Feb 25 '21 at 18:29
  • @Magnetron it doesn't contain app.UseHsts(); – studiobram Feb 25 '21 at 18:31
  • First trace down who sent the 307 response, https://learn.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-failed-requests-using-tracing-in-iis – Lex Li Feb 25 '21 at 18:41
  • Set up your logging to Debug and see when the redirect is happening. – Paulo Morgado Feb 25 '21 at 18:57
  • Does your IIS have rewrite module for https redirection or something like that? – Amir Feb 25 '21 at 19:03
  • @Amir The URL Rewrite module is installed, it isn't configured – studiobram Feb 25 '21 at 19:07
  • Just disable or delete it for testing and recheck! – Amir Feb 25 '21 at 19:12
  • @Amir disabling it doesn't work – studiobram Feb 25 '21 at 19:34
  • @LexLi Failed-Request Tracing or logging doesn't seem to work for some reason – studiobram Feb 25 '21 at 19:34
  • Do you have something like curl (or somehow) installed to double-check the request and for me? I want the log of response like this: curl [Your Address] -v – Amir Feb 25 '21 at 19:39
  • @Amir This is what I get when i run the command * Rebuilt URL to: http://api.projectname.dev/ * Trying 127.0.0.1... * TCP_NODELAY set * Connected to api.projectname.dev (127.0.0.1) port 80 (#0) > GET / HTTP/1.1 > Host: api.projectname.dev > User-Agent: curl/7.55.1 > Accept: */* > < HTTP/1.1 404 Not Found < Transfer-Encoding: chunked < Server: Microsoft-IIS/10.0 < Date: Thu, 25 Feb 2021 20:01:41 GMT < * Connection #0 to host api.projectname.dev left intact – studiobram Feb 25 '21 at 20:04
  • I can't see any redirection :|. Am I correct? – Amir Feb 25 '21 at 22:38
  • The problem was that I was using a .dev domain https://stackoverflow.com/questions/25277457/google-chrome-redirecting-localhost-to-https – studiobram Feb 27 '21 at 15:22
  • The problem was that I was using a .dev domain https://stackoverflow.com/questions/25277457/google-chrome-redirecting-localhost-to-https – studiobram Feb 27 '21 at 15:26

1 Answers1

-1

Remove the line UseHttpsRedirection() from your Program or Startup.cs

Amir
  • 1,214
  • 7
  • 10