.net 出于测试目的,管理器将绕过它 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, errors) => true;

.net 出于测试目的,管理器将绕过它 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, errors) => true;,.net,authentication,encryption,ssl,.net,Authentication,Encryption,Ssl,下面是一种将SSL认证绑定到IP/端口组合的替代方法,无需使用httpcfg.exe(XP)或netsh.exe(Vista+) 的要点是,你可以使用C++ API来构建Windows,而不是通过命令行来编程,从而消除对操作系统的依赖性,并安装了HTTPCFG。请随意使用它。 HttpCfg.exe set ssl -i 0.0.0.0:801 -h 35c65fd4853f49552471d2226e03dd10b7a11755 LPCTSTR pszX500 = subject; DWO

下面是一种将SSL认证绑定到IP/端口组合的替代方法,无需使用
httpcfg.exe
(XP)或
netsh.exe
(Vista+)


的要点是,你可以使用C++ API来构建Windows,而不是通过命令行来编程,从而消除对操作系统的依赖性,并安装了HTTPCFG。请随意使用它。

HttpCfg.exe set ssl -i 0.0.0.0:801 -h 35c65fd4853f49552471d2226e03dd10b7a11755
LPCTSTR pszX500 = subject;
DWORD cbEncoded = 0;
CertStrToName(X509_ASN_ENCODING, pszX500, CERT_X500_NAME_STR, NULL, pbEncoded, &cbEncoded, NULL);
pbEncoded = (BYTE *)malloc(cbEncoded);
CertStrToName(X509_ASN_ENCODING, pszX500, CERT_X500_NAME_STR, NULL, pbEncoded, &cbEncoded, NULL);

// Prepare certificate Subject for self-signed certificate
CERT_NAME_BLOB SubjectIssuerBlob;
memset(&SubjectIssuerBlob, 0, sizeof(SubjectIssuerBlob));
SubjectIssuerBlob.cbData = cbEncoded;
SubjectIssuerBlob.pbData = pbEncoded;

// Prepare key provider structure for self-signed certificate
CRYPT_KEY_PROV_INFO KeyProvInfo;
memset(&KeyProvInfo, 0, sizeof(KeyProvInfo));
KeyProvInfo.pwszContainerName = _T("my-container");
KeyProvInfo.pwszProvName = NULL;
KeyProvInfo.dwProvType = PROV_RSA_FULL;
KeyProvInfo.dwFlags = CRYPT_MACHINE_KEYSET;
KeyProvInfo.cProvParam = 0;
KeyProvInfo.rgProvParam = NULL;
KeyProvInfo.dwKeySpec = AT_SIGNATURE;

// Prepare algorithm structure for self-signed certificate
CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm;
memset(&SignatureAlgorithm, 0, sizeof(SignatureAlgorithm));
SignatureAlgorithm.pszObjId = szOID_RSA_SHA1RSA;

// Prepare Expiration date for self-signed certificate
SYSTEMTIME EndTime;
GetSystemTime(&EndTime);
EndTime.wYear += 5;

// Create self-signed certificate
pCertContext = CertCreateSelfSignCertificate(NULL, &SubjectIssuerBlob, 0, &KeyProvInfo, &SignatureAlgorithm, 0, &EndTime, 0);
hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"MY");
CertAddCertificateContextToStore(hStore, pCertContext, CERT_STORE_ADD_REPLACE_EXISTING, 0);
CRYPT_KEY_PROV_INFO KeyProvInfo;
memset(&KeyProvInfo, 0, sizeof(KeyProvInfo));
KeyProvInfo.pwszContainerName = _T("my-container");
KeyProvInfo.pwszProvName = _T("Microsoft RSA SChannel Cryptographic Provider");
KeyProvInfo.dwProvType = PROV_RSA_SCHANNEL;
KeyProvInfo.dwFlags = CRYPT_MACHINE_KEYSET;
KeyProvInfo.cProvParam = 0;
KeyProvInfo.rgProvParam = NULL;
KeyProvInfo.dwKeySpec = AT_KEYEXCHANGE;
netsh http add sslcert ipport=0.0.0.0:1234 certhash=613bb67c4acaab06def391680505bae2ced4053b  appid={86476d42-f4f3-48f5-9367-ff60f2ed2cdc}
 HttpListener listener = new HttpListener();
 listener.Prefixes.Add("https://+:1234/");
 listener.Start();
 Console.WriteLine("Listening...");
 HttpListenerContext context = listener.GetContext();

 using (Stream stream = context.Response.OutputStream)
 using (StreamWriter writer = new StreamWriter(stream))
     writer.Write("hello, https world");

 Console.ReadLine();
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, errors) => true;