https改为http

生成证书

  1. 创建文件:Create-3YearCert.ps1
powershell 复制代码
# 生成 3 年有效期的自签名证书并信任
# 使用前请修改以下三个变量为你需要的值
$CertPassword = "XXXX656"   # 证书私钥密码(务必修改为强密码)
$ExportPath   = "D:\yksk-3year-cert.pfx"  # 证书导出路径
$CertSubject  = "CN=YKSKWIPCert-3Year"   # 证书主题,可自定义

# 生成证书(有效期 3 年)
$cert = New-SelfSignedCertificate `
    -Subject $CertSubject `
    -CertStoreLocation "Cert:\CurrentUser\My" `
    -KeyExportPolicy Exportable `
    -KeySpec Signature `
    -KeyUsage DigitalSignature, KeyEncipherment `
    -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1") `
    -FriendlyName "YKSK WIP  3-Year Certificate" `
    -NotAfter (Get-Date).AddYears(3)   # 设置 3 年后过期

# 导出为 PFX 文件(包含私钥)
$securePassword = ConvertTo-SecureString -String $CertPassword -Force -AsPlainText
Export-PfxCertificate -Cert "Cert:\CurrentUser\My\$($cert.Thumbprint)" `
    -FilePath $ExportPath -Password $securePassword

Write-Host "✅ 证书已导出到: $ExportPath" -ForegroundColor Green

# 安装到受信任的根存储区(使浏览器/系统信任)
$rootStore = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root", "CurrentUser")
$rootStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$rootStore.Add($cert)
$rootStore.Close()

Write-Host "✅ 证书已添加到受信任的根证书存储区" -ForegroundColor Green
Write-Host "🎉 操作完成!证书有效期为 3 年。" -ForegroundColor Cyan
  1. 以管理员身份运行powershell,后生成 yksk-3year-cert.pfx
powershell 复制代码
D:
.\Create-3YearCert.ps1
  1. 通过bat文件进行安装证书install_cert.bat
bash 复制代码
@echo off
chcp 65001 >nul
cd /d "%~dp0"

set PFX_FILE=yksk-3year-cert.pfx
set PFX_PASS=XXXX656

if not exist "%PFX_FILE%" (
    echo Error: Certificate file "%PFX_FILE%" not found.
    echo Current directory: %cd%
    pause
    exit /b 1
)

net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Please run this script as Administrator.
    pause
    exit /b 1
)

echo Installing certificate to Personal and Trusted Root stores...

powershell -ExecutionPolicy Bypass -Command ^
    "$pfxPath = '%PFX_FILE%'; " ^
    "$password = '%PFX_PASS%'; " ^
    "try { " ^
    "    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2; " ^
    "    $cert.Import($pfxPath, $password, 'Exportable,PersistKeySet'); " ^
    "    $storeMy = New-Object System.Security.Cryptography.X509Certificates.X509Store('My', 'CurrentUser'); " ^
    "    $storeMy.Open('ReadWrite'); " ^
    "    $storeMy.Add($cert); " ^
    "    $storeMy.Close(); " ^
    "    Write-Host '[OK] Certificate added to Personal store.' -ForegroundColor Green; " ^
    "    $storeRoot = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root', 'CurrentUser'); " ^
    "    $storeRoot.Open('ReadWrite'); " ^
    "    $storeRoot.Add($cert); " ^
    "    $storeRoot.Close(); " ^
    "    Write-Host '[OK] Certificate added to Trusted Root store.' -ForegroundColor Green; " ^
    "    Write-Host 'Installation completed successfully.' -ForegroundColor Green; " ^
    "} catch { " ^
    "    Write-Host 'Error: ' $_.Exception.Message -ForegroundColor Red; " ^
    "    exit 1; " ^
    "}"

if %errorlevel% equ 0 (
    echo.
    echo All operations completed. Certificate is valid for 3 years.
) else (
    echo.
    echo Installation failed. Please try manual import:
    echo   1. Double-click %PFX_FILE%
    echo   2. Select "Local Machine" and click Next
    echo   3. Enter password: %PFX_PASS%
    echo   4. Choose "Place all certificates in the following store"
    echo   5. Click Browse and select "Trusted Root Certification Authorities"
    echo   6. Finish the wizard
)

pause
  1. 以管理员身份运行install_cert.bat,安装证书

第二步:在你的ASP.NET Core项目中配置使用这个证书文件

现在,修改你的项目(例如 Program.cs),使其明确地加载并使用你刚生成的 .pfx 文件 。

部署证书文件:将第一步生成的 my-longterm-cert.pfx 文件连同你的应用程序一起发布。

csharp 复制代码
using System.Security.Cryptography.X509Certificates;

var builder = WebApplication.CreateBuilder(args);

// ... 其他服务配置 ...

// 配置 Kestrel 服务器使用特定的证书文件
builder.WebHost.ConfigureKestrel((context, options) =>
{
    // 获取证书文件的路径,建议将路径放在配置文件或环境变量中
    // 例如:在 appsettings.json 中定义 "CertPath": "Certs/my-longterm-cert.pfx"
    var certPath = Path.Combine(builder.Environment.ContentRootPath, "Certs", "my-longterm-cert.pfx");
    var certPassword = "YourStrongPassword123"; // ⚠️ 重要:生产环境请使用安全配置存储密码

    if (File.Exists(certPath))
    {
        options.ConfigureHttpsDefaults(httpsOptions =>
        {
            httpsOptions.ServerCertificate = new X509Certificate2(certPath, certPassword);
        });
    }
    else
    {
        // 处理证书文件不存在的情况,例如抛出异常或回退到默认开发证书
        throw new FileNotFoundException($"HTTPS 证书文件未找到: {certPath}");
    }
});

var app = builder.Build();

// ... 中间件配置 (app.UseHttpsRedirection() 等) ...

app.Run();

查看与管理证书

如果你想查看这张证书的具体过期日期或进行管理,可以通过操作系统的证书管理器来操作。

在 Windows 上:

  1. 按下 Win + R 键,输入 certmgr.msc 并回车,打开"当前用户"证书管理器。
  2. 导航到 "受信任的根证书颁发机构" -> "证书" 文件夹。
  3. 在列表中找到颁发者为 localhost 的证书,双击它即可在"有效期"字段中看到确切的起止时间。

通用清理命令:如果遇到证书问题需要彻底重置,可以使用 dotnet dev-certs https --clean 命令先清除所有现有的 HTTPS 开发证书,然后再重新生成并信任。

相关推荐
babe小鑫3 小时前
高职大数据管理与应用专业学习数据分析的价值
学习·数据挖掘·数据分析
向哆哆4 小时前
粉尘环境分类检测千张图数据集(适用YOLO系列)(已标注+划分/可直接训练)
yolo·分类·数据挖掘
k7Cx7e17 小时前
宝塔域名强制SSL和带www的方法
网络·网络协议·ssl
沪漂阿龙18 小时前
大模型选型决策全流程:从需求分析到生产上线的六步法
人工智能·数据挖掘·需求分析
qq_316837751 天前
使用 certbot docker镜像生成阿里云域名ssl证书
阿里云·docker·ssl
AI前沿晓猛哥1 天前
2026最新卸载工具对比:哪个好?从功能、安全、捆绑三个维度全面评测
数据挖掘
文静小土豆1 天前
CentOS 7 升级 OpenSSL 3.5.4 详细指南
linux·运维·centos·ssl
biubiubiu07061 天前
Certbot 申请SSL证书的三种方式详解(Ubuntu 22.04环境)
网络·网络协议·ssl
2501_943695331 天前
高职数据可视化技术专业,怎么提升数据可视化的设计审美?
信息可视化·数据挖掘·数据分析