Signing Soap Message with X509 Certificate
Digitally Sign from C# - SOAP MessageIn this example we will see how we recover a certificate from a location on disk, add the certificate to the certificate store, open the certificate store, we take the certificate and sign the soap message.
For this we use Microsoft.Web.Services2.
Below an example:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography.X509Certificates;
namespace proxy
{
public class DigitalSignature
{
public static getDataResponse queryingData(string name)
{
proxy.BanWS conexion = new proxy.Banws();
//VALIDATION OF CONNECTION V3
X509Certificate2 elCert = new X509Certificate2(@"C:\portecle-1.5\12345.P12", "12345");
conexion.ClientCertificates.Add(elCert);
// Copy the certificate to the certificate store using ASPNET
// spent the path and password
X509Certificate2 certificate = new X509Certificate2(@"C:\portecle-1.5\12345.P12", "12345");
X509Store stores = new X509Store(StoreName.My, StoreLocation.CurrentUser);
stores.Open(OpenFlags.ReadWrite);
stores.Add(certificate);
stores.Close();
String sto = X509CertificateStore.MyStore;
// Open the Certificates Stores
X509CertificateStore store = X509CertificateStore.CurrentUserStore(sto);
store.OpenRead();
// We look for the certificate that we will use to perform the signature
String certname = "conticert";
Microsoft.Web.Services2.Security.X509.X509CertificateCollection certcoll = store.FindCertificateBySubjectString(certname);
if (certcoll.Count != 0)
{
Microsoft.Web.Services2.Security.X509.X509Certificate cert = certcoll[0];
SoapContext ctx = conexion.RequestSoapContext;
SecurityToken tok = new X509SecurityToken(cert);
ctx.Security.Timestamp.TtlInSeconds = 120;
ctx.Security.Tokens.Add(tok);
// We signed the request
ctx.Security.Elements.Add(new MessageSignature(tok));
}
//remote call
getDataResponse response = new getDataResponse();
response = conexion.getData(name);
return response;
}
}
}
0 comentarios:
Post a Comment