Blazor/IIS Http强制重定向到无法使用重写规则的Http

pftdvrlh  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(148)

问题
我有一个使用IIS托管的Blazor应用程序。我遇到了一个问题,即任何未显式使用https的链接都显示404,并且不指向https
例如,如果您转到:

app.domain.com -> 404  
http://app.domain.com -> 404  
https://app.domain.com -> Successful

我已经尝试过的事情

我从其他答案和本指南中了解了IIS URL重写规则:
NamecheapHow to force HTTPS using a web.config file
根据this answer取消选中并选中ISS设置中的“Require SSL”(需要SSL)
在web.config中更改URL以硬编码URL域并使用R:1,

url="https://app.domain.com/{R:1}"

而不是

url="https://{HTTP_HOST}{REQUEST_URI}"

每个this answer
添加应用程序。UseHttpsRedirection();到Program.cs和

builder.Services.AddHttpsRedirection(options =>
    {
        options.RedirectStatusCode = (int)HttpStatusCode.PermanentRedirect;
        options.HttpsPort = 443;
    });

每个Enforce HTTPS in ASP.NET Core

我的当前代码

下面是我的重写规则:

<rewrite>
        <rules>
            <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="SeeOther" />
            </rule>
        </rules>
    </rewrite>

和我的程序。cs:

// <<<< BUILDER.SERVICES.ADDSINGLETON... >>>>
    builder.Services.AddResponseCompression(o =>
    {
        o.EnableForHttps = true;
        o.Providers.Add<BrotliCompressionProvider>();
        o.Providers.Add<GzipCompressionProvider>();
        o.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
            new[] { "image/svg+xml" });
    });
    builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Optimal;
    });
    builder.Services.Configure<GzipCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Optimal;
    });

    var app = builder.Build();
    // <<<<  APP.SERVICES.GetRequiredService... >>>>

    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment() && !app.Environment.IsStaging())
    {
        app.UseExceptionHandler("/Error");
        // 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();

    if(!app.Environment.IsProduction())
    {
        StaticWebAssetsLoader.UseStaticWebAssets(app.Environment, app.Configuration);
    }

    app.UseRouting();
    app.UseResponseCompression();

    app.MapBlazorHub();
    app.MapFallbackToPage("/_Host");

    app.Run();

谢谢你的帮助

wribegjk

wribegjk1#

我为站点添加了一个端口80绑定,在那里我只有443个每Bob Cares。这修复了这个问题!!!

相关问题