如何在带有身份的ASP.NET Core项目中禁用HTTPS?

代码和芯片

我最近在Visual Studio 2019中创建了一个ASP.NET Core 3.0 Web应用程序项目(已启用Docker,但我认为这并不重要),并且在包含ASP.NET Identity时似乎无法禁用HTTPS个人用户帐户。每当我在调试器中启动该应用程序时,该页面就会使用HTTPS打开,并导航至HTTP会再次将我重定向。

我尝试创建一个新项目以测试发生了什么,并且发现了以下内容:

创建项目时,我启用了使用ASP.NET Identity的功能,以便在我的应用程序中使用本地帐户,并且我注意到,如果再次执行向导以创建具有相同设置的项目(启用了Identity的Web应用程序(对于单个用户帐户),取消选中启用HTTPS复选框的选项显示为灰色。

无法使用身份创建项目时禁用HTTP

创建项目后,我尝试Startup.cs通过注释掉相关行来禁用HTTPS重定向中间件

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

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }

并进行了编辑,launchSettings.json以尝试使应用程序在调试时使用HTTP而不是HTTPS运行,但是该应用程序仍尝试使用HTTPS进行加载。

launchSettings.json 之前:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:51767",
      "sslPort": 44396
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "HTTPSTest": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "environmentVariables": {
        "ASPNETCORE_URLS": "https://+:443;http://+:80",
        "ASPNETCORE_HTTPS_PORT": "44397"
      },
      "httpPort": 51777,
      "useSSL": true,
      "sslPort": 44397
    }
  }
}

launchSettings.json 后:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:51767"
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "HTTPSTest": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:5000"
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "environmentVariables": {
      },
      "httpPort": 51777
    }
  }
}

有人对我接下来可以尝试的东西有任何想法吗?我希望该应用程序位于SSL终止的反向代理后面,因此我认为禁用此功能并不是没有道理的。

代码和芯片

So I actually managed to fix this. The issue I was seeing with Visual Studio opening up the app using HTTPS was an issue with Visual Studio.

Running through the same setup on another computer worked just fine by commenting out the app.UseHttpsRedirection() line in Startup.cs and removing the SSL references in launchSettings.json as in my original post - running the debugger opened up the page using HTTP and was able to connect to the app.

In order to get everything working again, I tried removing Visual Studio completely, rebooting, reinstalling and trying again, but still had the same issue. In the end I had to also remove all the Visual Studio folders under AppData in my user directory and THEN reinstall Visual Studio, and then it was finally working again when I tried debugging my project!

UPDATE:
This actually came back after the weekend, and I discovered that this time my problem was that HSTS had cached the use of HTTPS in Chrome. Examples of how to remove this for different browsers can be found here: https://appuals.com/how-to-clear-or-disable-hsts-for-chrome-firefox-and-internet-explorer/

In summary, for Chrome:
1. Navigate to chrome://net-internals/#hsts
2. In the Delete domain security policies section, enter localhost as the domain and hit the Delete button

更新2-警告尽管此“修复”允许我使用HTTP运行我的项目,但我确实发现除非启用HTTPS,否则Identity不允许我登录-我认为这与SignInManager设置仅HTTPS身份验证有关cookie-有关更多详细信息,请参见此处:AspNet核心标识-未在生产环境中设置cookie

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过dotnet将所有身份文件放入ASP.NET Core 2.1 MVC项目中?

如何在.Net Core项目中禁用TypeScript编译?

当使用ASP.NET Core Web API中没有身份的cookie身份验证时,如何在登录时刷新CSRF令牌

带有身份的ASP.NET:实体框架在SaveChanges上引发System.Data.Entity.Core.UpdateException

在带有身份服务器的 asp.net core angular SPA 中,SignalR 授权无法开箱即用

在 ASP.NET Core 项目中禁用 jQuery DatePicker 的过去日期

创建没有身份验证的项目时的ASP.NET身份集成

没有身份的ASP.NET Core 2.0承载身份验证

如何在.NET Core项目中引用.NET Framework项目?

将具有身份的集合添加到ASP.NET Core中的用户对象

具有身份和ExternalLogin的ASP.NET Core 3.1中基于角色的授权

ASP.NET Core上具有身份的工作单元

ASP.NET Core 禁用 HTTPS

如何将ASP.NET Core身份添加到现有Core mvc项目中?

如何从ASP.NET Core项目中删除Docker支持?

如何在.NET Core项目中添加C ++库

如何在ASP.NET Core(面向.NET Framework的目标)项目中引用旧程序集?

ASP.NET Core 2.1 + Kestrel(如何禁用HTTPS)

如何在带有Servicestack的.NET Core中使用Windows身份验证登录

如何在 asp.net 核心 WebApi 项目中使用带有 swagger 的 OData 过滤器?

空ASP.Net Core项目中的JavaScript Intellisense

在测试项目中使用ASP.NET Core的ConfigurationBuilder

如何在Visual Studio ASP.NET Core项目中使用语义UI反应?

如何在ASP.NET Core项目中读取本地文件?

如何在ASP.NET Core项目中为MVC和SignalR配置路由?

如何在ASP.NET Core项目中包括对程序集的引用

如何在ASP.NET Core React / Redux模板项目中更新我的React版本?

如何在Visual Studio的ASP.NET Core项目中衡量代码覆盖率?

如何在我的ASP.NET Core MVC项目中引用Microsoft.JQuery.Unobtrusive.Ajax