설치된 인증서로 XML 문서에 서명하는 데 문제가 있습니다. 인증서 파일 (.pfx)을 사용하여 X509Certificate2의 LocalMachine, CurrentUser 및 Initialize 인스턴스에 설치된 인증서로 시도했습니다. 각각에는 그것의 자신의 문제점이있다; 내 기본 설정은 LocalMachine 저장소에 설치된 인증서를 사용하는 것입니다. 나는 세 가지 방법과 각각 우려 설명했다 아래 :X.509 인증서가 설치된 xml 문서에 서명
StoreLocation LocalMachine - 선호하는 방법을
var certStore = new X509Store(StoreLocation.LocalMachine);
certStore.Open(OpenFlags.ReadOnly);
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbPrint, true);
var myCert = certCollection[0];
// I can get the correct certificate but the following line throws "Invalid provider type specified." error
var SigningKey = myCert.PrivateKey;
StoreLocation CurrentUser
var certStore = new X509Store(StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbPrint, true);
var myCert = certCollection[0];
// I can get the correct certificate but the following line throws "Keyset does not exist" error
var SigningKey = myCert.PrivateKey;
내가 만약 내가 단지의 PrivateKey를 얻을 수 있습니다 사용 권한을 % ALLUSERSPROFILE % \ Application Data \ Microsoft \ Crypto \ RSA \ MachineKeys 폴더로 변경하십시오. 이것은 서명을 구현하는 올바른 방법으로 보이지 않습니다. 인증서 파일
var certificateFile = @"C:\CertificateFolder\AuthorizedCertificate.pfx";
var myCert = new X509Certificate2(certificateFile, password, X509KeyStorageFlags.UserKeySet);
이 방법을 사용하여
그러나 내가 인증서 파일과으로 바람직하지 않은 암호를 제공해야 할 것 작동합니다.
첫 번째 방법 (LocalMachine)을 작동 시키려면 어떻게해야합니까? 이 작업을 수행하는 데 권장되는/최선의 방법은 무엇입니까?
은 참고 다음 코드는
private void SignXml(XmlDocument xmlDoc, X509Certificate2 cert)
{
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc);
// Add the key to the SignedXml document.
signedXml.SigningKey = cert.PrivateKey;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
var env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Include the public key of the certificate in the assertion.
signedXml.KeyInfo = new KeyInfo();
signedXml.KeyInfo.AddClause(new KeyInfoX509Data(cert, X509IncludeOption.WholeChain));
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}
설명해 주셔서 감사합니다. myCert.GetRSAPrivateKey() .Net Framework 4.6.1과 함께 작업했지만 코드 (signedXml.ComputeSignature())의 다른 부분에서 다음 오류가 발생하여 도움을 받기를 바랍니다. System.NotSupportedException 메서드가 지원되지 않습니다. System.Security.Cryptography.RSA.DecryptValue (Byte [] rgb)에서 Byte [] DecryptValue (Byte []) at System.Security.Cryptography.Xml.SignedXml.ComputeSignature() – imran
@imran 4.6.2을 필요로합니다. 대답에서 설명했다. – bartonjs