gaja

gaja

  • NA
  • 1
  • 0

Add signature to the soap request sent from a C#.net client to the web service.

Feb 8 2008 12:53 AM

I have client code written in C#.net2003 to access secure web service. Im using the proxy class which is generated using "Add Web Reference" option. The code generates the signature which is to be sent as part of soap request to the web service. Can anyone guide me ,why im getting the below error.and how to track to the outgoing request.

please refer the code below and im getting error like"The underlying connection was closed:Could not establish trust relationship with remote server."

SoapContext requestContext = testObj.RequestSoapContext;

UsernameToken unt = new UsernameToken("un","pwd",PasswordOption.SendPlainText);

testObj.RequestSoapContext.Security.Tokens.Add(unt);

testObj.RequestSoapContext.Security.MustUnderstand = false;

testObj.RequestSoapContext.Path.MustUnderstand = false;

X509CertificateStore myStore= X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);

myStore.OpenRead();

X509Certificate cert = (X509Certificate)myStore.Certificates[0];

X509SecurityToken x509certToken = new X509SecurityToken(cert);

SecurityToken certToken = (SecurityToken)x509certToken;

testObj.RequestSoapContext.Security.Tokens.Add(certToken);

XmlDocument soapDoc=new XmlDocument();

soapDoc.LoadXml(inputXml);

Signature sign =new Signature(certToken);

SignedXml signedXml = new SignedXml(soapDoc);

Reference reference = new Reference();

reference.Uri = "#Body";

signedXml.AddReference(reference);

SYSSE.RSA key = SYSSE.RSA.Create();

string pKey = key.ToXmlString(true);

XmlDocument keyPairDoc=new XmlDocument();

SYSSE.RSA priKey = SYSSE.RSA.Create();

priKey.FromXmlString(pKey);

signedXml.SigningKey =priKey;

signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationUrl;

signedXml.ComputeSignature();

sign.LoadXml(signedXml.GetXml());

testObj.RequestSoapContext.Security.Elements.Add(sign);

TextBox1.Text = testObj.testConnection("Hello");