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 开发证书,然后再重新生成并信任。

相关推荐
laoli_coding4 小时前
如何配置HTTPS站点访问的免费SSL域名证书
网络协议·https·node.js·ssl
科技大视界11 小时前
投资AI项目,传统尽调不够用了——李章虎律师拆解算法、数据、算力三大雷区
人工智能·算法·数据挖掘
糖果店的幽灵12 小时前
安全测试从入门到精通_网络基础与HTTPS
网络·https·php
数据知道15 小时前
HTTP/HTTPS 安全深度剖析:抓包、解密与证书陷阱
安全·http·https·mitmproxy·抓包解密
2501_9151063218 小时前
iOS 软件测试工具性能监控、日志分析 KeyMob、Instruments等
android·ios·小程序·https·uni-app·iphone·webview
STLearner1 天前
ICML 2026 | LLM×Graph论文总结[1]【图基础模型,文本属性图,多模态属性图,图对齐,图提示学习,关系深度学习
论文阅读·人工智能·python·深度学习·学习·机器学习·数据挖掘
m0_547486662 天前
《面向对象分析设计与建模》全套PPT课件
人工智能·数据挖掘·uml
qq_355357022 天前
SKC智能知识管理系统-设计分享 之 知识聚类图谱
机器学习·数据挖掘·聚类
ClickHouseDB2 天前
简化 Langfuse 的规模化应用
人工智能·数据挖掘
韩曙亮2 天前
【Flutter】iOS 网络权限设置 ② ( 配置 iOS 平台可使用 HTTP 访问 | ATS 网络安全强制规范 )
网络·flutter·http·ios·https·网络权限·ats