【问题标题】:Getting error with SSL on dotnet Razor application - 'this ca root certificate is not trusted'在 dotnet Razor 应用程序上出现 SSL 错误 - “此 ca 根证书不受信任”
【发布时间】:2023-03-05 13:17:01
【问题描述】:

我已经在我的 startup.cs 文件中输入了几个命令,这些命令应该可以让我进行 HTTPS 访问。我的代码是:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

我也试过运行这个命令:

dotnet dev-certs https -v

但是,该命令告诉我我已经有一个证书。我的页面将我从 localhost:5000 重定向到 localhost:5001,但它不安全

当点击 url 中的“不安全”并点击证书时,我得到以下窗口:

我似乎根本找不到问题,我不知道如何解决它,有人有任何解决方案吗?

【问题讨论】:

    标签: asp.net .net ssl razor razor-pages


    【解决方案1】:

    您的 Configure 方法缺少一些内容。您可能需要放置一些东西,例如 UseHttpsRedirection 等。

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler();
        }
        app.UseHsts(); //is a security feature to force SSL.
        app.UseAuthentication();//authentication middleware 
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
    

    您还需要在 launchSettings.json 文件中进行更改。

    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:5001",
          "sslPort": 44300
        }
      },
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "YOUR-PROJECT": {
          "commandName": "Project",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "dotnetRunMessages": "true",
          "applicationUrl": "https://localhost:5001;http://localhost:5001"
        }
      }
    }
    

    注意“applicationUrl”(http 和 https url)和“sslPort”(44300 - 44399)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2016-08-22
      相关资源
      最近更新 更多