我需要使用C#创建一个自签名证书(用于本地加密 - 它不用于保护通信).
我已经看到一些使用P/Invoke和Crypt32.dll的实现,但是它们很复杂并且很难更新参数 - 我还想尽可能避免P/Invoke.
我不需要跨平台的东西 - 只在Windows上运行对我来说已经足够了.
理想情况下,结果将是一个X509Certificate2对象,我可以使用该对象插入Windows证书存储区或导出到PFX文件.
在为自主主机Owin控制台应用程序设置HTTP时出现问题.浏览器始终显示连接重置错误.
我已尝试根据此链接http://chavli.com/how-to-configure-owin-self-hosted-website-with-ssl/手动创建证书,但我仍然在该端口上遇到连接重置问题.我已经检查了Windows事件日志,并且没有错误消息.
应用程序将自行创建X509证书并自动运行netsh命令.
没有Ssl,应用程序可以正确显示网页.
任何人都可以在下面运行我的代码并查看它是否可以在您的计算机上运行?提前致谢.
需要添加COM引用CertEnroll 1.0 Type Library来编译下面的代码(vs2015已在类型库中包含此COM引用)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using CERTENROLLLib;
using Microsoft.Owin.Hosting;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
namespace Owin.Startup
{
class Program
{
static void Main(string[] args)
{
int port = 8888;
string url = $"https://localhost:{port}";
var cert = GetCert("localhost", TimeSpan.FromDays(3650), "devpwd", AppDomain.CurrentDomain.BaseDirectory + "cert.dat");
ActivateCert(cert, port, GetAppId());
using (WebApp.Start<Startup>(url))
{
Console.WriteLine($"Hosted: {url}");
Console.ReadLine(); …Run Code Online (Sandbox Code Playgroud) 我想从每个设备的自签名证书切换到一对证书,其中一个是以前生成的,放置在受信任的根证书颁发机构存储中,对所有设备都相同,并作为第二个证书的根 CA 工作,该证书是按设备,并放置在个人存储中。
我不想使用 makecert,因为创建签名证书会显示 UI,这是我想避免的。此外,由于某些与许可证相关的内容(尽管我有可用的解决方案),因此无法使用 OpenSSL。所以,现在我正在使用基于 CertEnroll 库的小型 C# 工具。
这就是我为第一个根 CA 证书创建 pfx 的方式。
makecert -n "CN=Root CA" -cy authority -r -a sha256 -len 2048 -sv root.pvk root.cer
pvk2pfx -pvk root.pvk -spc root.cer -pfx root.pfx -pi 123 -po 123
Run Code Online (Sandbox Code Playgroud)
要从 C# 代码创建证书,我参考了问题如何以编程方式为 WCF 服务创建自签名证书?和C# 使用 certenroll.dll 生成一个没有 CA 的非自签名客户端 CX509Certificate 请求。
到目前为止,我有以下代码。证书生成方法:
/// <summary>
/// Generates self-signed certificate with specified subject, which will expire after specified timespan.
/// </summary>
public X509Certificate2 CreateCertificate(string subjectName, TimeSpan …Run Code Online (Sandbox Code Playgroud) c# ×2
ssl ×2
.net ×1
certenroll ×1
certificate ×1
https ×1
netsh ×1
owin ×1
self-hosting ×1
self-signed ×1