I'm trying to create signed XML document with SignedXml class. I'm using Framework 1.1 and WSE 2.0 to get certificate. I get certificate from LOCAL_MACHINE\My store but when I try get private key "oCertificado.Key.ToXmlString(True)" I get the next exception:
"An unhandled exception of type 'System.NotSupportedException'
occurred in microsoft.web.services2.dll Additional information: Export of private parameters is not supported"
Expand|Select|Wrap|Line Numbers
- Protected Function ObtenerCertificado() As X509Certificate
- Dim store As X509CertificateStore
- Try
- store = X509CertificateStore.LocalMachineStore (X509CertificateStore.MyStore)
- Dim open As Boolean = store.OpenRead()
- Dim oCertif As X509Certificate
- Dim oCertificado As X509Certificate
- For Each oCertif In store.Certificates
- If InStr(oCertif.GetName, "DESARROLLO.MINHAC.AGE") <> 0 Then
- oCertificado = oCertif
- End If
- Next
- Return oCertificado
- Catch ex As Exception
- Me.iEstadoOperacion = Me.AYUDA
- Me.sMsgCliente = ex.Message
- Me.sMsgCliente = Me.sMsgCliente.Replace("'", "")
- Finally
- store.Close()
- store = Nothing
- End Try
- End Function
- Protected Function FirmarXml(ByVal oCertificado As X509Certificate, ByVal DocumentoXml As XmlDocument) As XmlElement
- Dim signedXml As New System.security.Cryptography.xml.SignedXml
- Dim dataObject As New DataObject
- Try
- signedXml.SigningKey = oCertificado.Key
- Me.iEstadoOperacion = Me.AYUDA
- Me.sMsgCliente = oCertificado.Key.ToXmlString(False)
- Exit Function
- Dim document As New XmlDocument
- Dim node As XmlNode = document.CreateNode(XmlNodeType.Element, "", "MyElement", "samples")
- node.InnerText = DocumentoXml.OuterXml.ToString
- document.AppendChild(node)
- dataObject.Data = document.ChildNodes
- dataObject.Id = "MyObjectId"
- signedXml.AddObject(dataObject)
- ' Creamos una referencia
- Dim reference As New System.security.Cryptography.xml.Reference
- reference.Uri = "#MyObjectId"
- 'Añadimos la referencia.
- signedXml.AddReference(reference)
- 'Añadimos KeyInfo.
- Dim keyInfo As New KeyInfo
- keyInfo.AddClause(New RSAKeyValue(oCertificado.Key))
- keyInfo.AddClause(New KeyInfoX509Data(oCertificado))
- signedXml.KeyInfo = keyInfo
- 'Firmamos
- signedXml.ComputeSignature()
- Return signedXml.GetXml()
- Catch ex As Exception
- Me.iEstadoOperacion = Me.AYUDA
- Me.sMsgCliente = ex.Message
- Me.sMsgCliente = Me.sMsgCliente.Replace("'", "")
- Finally
- dataObject = Nothing
- signedXml = Nothing
- End Try
- End Function
Thank you very much.