我试图使用Request.ClientCertificate读取X509证书,但没有返回任何内容.证书肯定附加到请求,因为我可以从发送请求的页面获取证书信息.
我曾尝试从几个不同的地方阅读证书,但似乎无法让它工作.
我从这篇KB文章的代码开始.在请求的页面中,我尝试打印出有关证书的一些信息,但响应中没有返回任何内容.
这是在IIS 5.1上运行,并且通过SSL进行通信.这必须使用.Net框架的第2版完成
为什么证书似乎消失了?
我们使用C#代码我们用.p12文件构建X509Certificate2,在构造函数中我们插入证书的路径,证书的密码.我们还将其标记为可导出,如下所示:
X509Certificate2 x509Certificate2 = new X509Certificate2
("...\\MyCerificate.p12", "P@ssw0rd", X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)
我们通过以下方式获取私钥作为AsymmetricAlgorithm格式:
x509Certificate2.PrivateKey
Run Code Online (Sandbox Code Playgroud)
现在,我们希望从证书中获取私钥作为Base64格式 - 但我们不知道如何做到这一点,它对我们如此重要.
示例代码:
CspParameters cspParameters = new CspParameters();
cspParameters.ProviderType = 1; // PROV_RSA_FULL
// Create the crypto service provider, generating a new
// key.
mRsaCSP = new RSACryptoServiceProvider(mDefaultKeyLength, cspParameters);
mRsaCSP.PersistKeyInCsp = true;
RSAParameters privateKey = mRsaCSP.ExportParameters(true);
byte[] rsaBytes = mRsaCSP.ExportCspBlob(true);
try
{
X509Certificate2 cert = new X509Certificate2(rsaBytes);
mKeyDataPfx = Convert.ToBase64String(cert.Export(X509ContentType.Pkcs12, password));
}
catch (Exception ce)
{
string error = ce.Message;
}
Run Code Online (Sandbox Code Playgroud) 我正在开发ASP.NET 4.0 Web应用程序,我想从X509Store读取当前用户证书.读取LocalMachine证书工作正常,但如果我将StoreLocation设置为CurrentUser,它会给我一个空集合.
以下代码工作正常:
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); // StoreLocation.CurrentUser
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
Run Code Online (Sandbox Code Playgroud)
我已经检查了我的个人商店(通过certmgr.mmc),我确信我有证书.
我错过了什么?(store.Certificates为空)
我有应用程序,我必须使用一个证书,需要一个来自提示窗口的引脚.
我有以下代码.
SecureString password = GetPassword();
X509Certificate2 certificate = GetCertificate();
var cspParameters = new CspParameters(1,
"ProviderName",
"KeyContainerName",
null,
password);
certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters);
Run Code Online (Sandbox Code Playgroud)
在控制台应用程序中一切正常,但是当我在Windows服务或从任务调度程序启动的控制台应用程序中运行该代码时,应用程序在该行上冻结.
certificate.PrivateKey = new RSACryptoServiceProvider(cspParameters);
Run Code Online (Sandbox Code Playgroud)
没有例外,没有进展.
我正在使用与应用程序相同的凭据运行Windows服务.
Windows 10/Windows Server 2012
你有什么想法有什么不对吗?
有没有方便的方法从使用.NET Core的 PEM格式的.p12证书导出私钥/公钥?没有在低级别操作字节?我用谷歌搜索了几个小时,几乎没有任何东西在.net核心中可用,或者它没有记录在任何地方..
我们有一个X509Certificate2
var cert = new X509Certificate2(someBytes, pass);
var privateKey = cert.GetRSAPrivateKey();
var publicKey = cert.GetRSAPublicKey();
// assume everything is fine so far
Run Code Online (Sandbox Code Playgroud)
现在我需要将密钥导出为两个单独的PEM密钥.我已经尝试PemWriter过BouncyCastle,但是这些类型与Core的System.Security.Cryptography不兼容..没有运气.
- -编辑 - -
换句话说,我找到了一种方法来写这个:
$ openssl pkcs12 -in path/to/cert.p12 -out public.pub -clcerts -nokeys
$ openssl pkcs12 -in path/to/cert.p12 -out private.key -nocerts
Run Code Online (Sandbox Code Playgroud)
有人有想法吗?
谢谢 ...
今天,一位同事遇到了另外一个与这些相关的错误!我发现这些标志在过去我自己真的很令人沮丧,因为如果你在实例化X509Certificate2对象或导出它们或将它们保存在X509Store中时会略微出错,你可能会遇到各种各样奇怪的错误,例如:
是的,它们是有记录的,而且所有文档(以及一些文档似乎都有意义),但为什么它必须这么复杂?
我有证书someCert.cer。我使用 MMC 实用程序将其导入到本地证书存储中。我的 C# 应用程序可以使用以下代码访问它:
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
X509Certificate2 certificate = null;
store.Open(OpenFlags.ReadOnly);
try
{
var certificateCollection = store.Certificates.Find(X509FindType.FindByThumbprint, "THUMBPRINT", false);
certificate = certificateCollection[0];
}
finally
{
store.Close();
}
Run Code Online (Sandbox Code Playgroud)
该应用程序公开 TCP 套接字,当我尝试使用客户端应用程序连接到它时,出现异常:
The server mode SSL must use a certificate with the associated private key
事实上,我的证书的 PrivateKey 属性是空的。我导入的证书是否不正确,或者我应该在将证书导入商店之前对证书进行一些处理吗?
我的服务器身份验证代码如下所示:
stream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true);
Run Code Online (Sandbox Code Playgroud) 我有以下代码从Azure Key Vault下载证书。
$secretName = "TestCert"
$kvSecret = Get-AzureKeyVaultSecret -VaultName $vaultName -Name $certificateName
$kvSecretBytes = [System.Convert]::FromBase64String($kvSecret.SecretValueText)
$certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$certCollection.Import($kvSecretBytes,$null, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
Run Code Online (Sandbox Code Playgroud)
但是,在将cert导入certCollection时,import方法抛出以下错误。
Exception calling "Import" with "3" argument(s): "Cannot find the requested object.
"
At C:\Users\abc\Desktop\test2.ps1:8 char:1
+ $certCollection.Import($kvSecretBytes,$null,[System.Security.Cryptogr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CryptographicException
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助。谢谢
powershell x509certificate2 x509certificate x509 azure-keyvault
我想做的是创建一个有效的证书来签署电子文档。这需要带有私钥的 x509Certificate2 对象。我不知道如何使用我创建的 CA 证书创建带有私钥的签名证书。
如果可能的话,我想仅使用 .Net 代码来执行此操作(我使用的是 4.7.2)。
我的代码创建了一个带有私钥的有效CA证书,但是,子证书没有私钥,因此不能用于签署电子文档。
这是我现在拥有的代码......
private void CreateCerts()
{
const string issuerName = "Issuer";
const string signerName = "Signer";
var root = GetCertificate(issuerName, StoreName.AuthRoot);
if (root == null)
{
root = CreateRoot(issuerName);
}
var signer = GetCertificate(signerName, StoreName.My);
if (signer == null)
{
signer = CreateSigner(root, signerName);
}
}
private X509Certificate2 CreateSigner(X509Certificate2 root, string name)
{
X509Certificate2 signer = null;
using (RSA rsa = RSA.Create(2048))
{
CertificateRequest req = new CertificateRequest(
$"CN={name}",
rsa,
HashAlgorithmName.SHA256, …Run Code Online (Sandbox Code Playgroud) x509certificate2 ×10
c# ×8
.net ×2
asp.net ×2
private-key ×2
x509 ×2
.net-core ×1
base64 ×1
certificate ×1
pem ×1
powershell ×1
rsa ×1
security ×1
ssl ×1
windows ×1