如何在ASP.NET Core中实施HTTPS

时间:2019-04-15 06:24:54

标签: redirect asp.net-core cookies

我已经安装了 LetsEncrypt ,它也可以正常工作 在http和https中 但是我尝试将ASP.NET Core中的重定向HTTPS强制为docs.microsoft, 我使用 asp.net core 2.2 iis 10.0.14393.0 这是我的管道配置:

 services.Configure<CookiePolicyOptions>(options =>
         {

             options.CheckConsentNeeded = context => true;
             options.MinimumSameSitePolicy = SameSiteMode.None;
         });
        services.AddAuthentication(option =>
        {
           ..

        }).AddCookie(option =>
        {
            option.AccessDeniedPath = "/Account/Denied";
            option.LoginPath = "/Account/Login";
            ...


        });
        services.AddHttpsRedirection(options =>

        {

            options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
            options.HttpsPort = 443;

        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

和在Configure方法中:

  if (env.IsDevelopment())
        {

            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/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.UseAuthentication();
        app.UseStaticFiles(new StaticFileOptions()
        {
            OnPrepareResponse = context =>
            {
                context.Context.Response.Headers["Cache-Control"] =
                        "private, max-age=43200";

                context.Context.Response.Headers["Expires"] =
                        DateTime.UtcNow.AddDays(30).ToString("R");
            }
        });
        app.UseCookiePolicy();
        app.Use(async (context, next) =>
        {
            // For GetTypedHeaders, add: using Microsoft.AspNetCore.Http;
            context.Response.GetTypedHeaders().CacheControl =
                new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
                {
                    Public = true,
                    MaxAge = TimeSpan.FromDays(30)
                };
            context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Vary] =
                new string[] { "Accept-Encoding" };

            await next();
        });
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

但是我在Firefox中得到了这个错误:

  

页面重定向不正确

我在哪里错了?

1 个答案:

答案 0 :(得分:-1)

您是否尝试清除浏览器缓存? 如果您在更改后不清除缓存,浏览器通常会使用缓存中的旧重定向。