Apply WSS to SOAP request

sonal khatri 51 Reputation points
2024-07-01T16:29:10.02+00:00

Hi,

My Requirement is to apply outgoing WSS using RSA with SHA 256 using a certificate to the XML. Can you help me with how to achieve this?

XML request is attached.
xml request.txt

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,561 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 42,061 Reputation points Microsoft Vendor
    2024-07-02T08:29:53.7266667+00:00

    Hi @sonal khatri , Welcome to Microsoft Q&A,

    1. Loading RSA private key and certificate
    using System;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    
    public class CryptoHelper
    {
        public static RSA LoadPrivateKey(string certPath, string certPassword)
        {
            X509Certificate2 certificate = new X509Certificate2(certPath, certPassword);
            return certificate.GetRSAPrivateKey();
        }
    }
    
    1. Create and sign a SOAP XML request
    using System;
    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;
    using System.Text;
    using System.Xml;
    
    public class SoapHelper
    {
        public static string SignXml(string xml, RSA privateKey)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
    
            SignedXml signedXml = new SignedXml(xmlDoc)
            {
                SigningKey = privateKey
            };
    
            Reference reference = new Reference
            {
                Uri = ""
            };
    
            reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
            signedXml.AddReference(reference);
    
            KeyInfo keyInfo = new KeyInfo();
            keyInfo.AddClause(new RSAKeyValue((RSA)privateKey));
            signedXml.KeyInfo = keyInfo;
    
            signedXml.ComputeSignature();
    
            XmlElement xmlDigitalSignature = signedXml.GetXml();
            xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
    
            return xmlDoc.OuterXml;
        }
    }
    
    public class Program
    {
        public static void Main()
        {
            string xmlRequest = @"the xml request";
    
            string certPath = "path/to/your_certificate.pfx";
            string certPassword = "your_certificate_password";
    
            RSA privateKey = CryptoHelper.LoadPrivateKey(certPath, certPassword);
            string signedXml = SoapHelper.SignXml(xmlRequest, privateKey);
    
            Console.WriteLine(signedXml);
        }
    }
    
    1. Send a signed request over a WSS connection

    Using a WebSocket client library, such as WebSocketSharp, you can send a signed request.

    using System;
    using WebSocketSharp;
    
    public class Program
    {
        public static void Main()
        {
            string signedXml = "<your_signed_xml>";
    
            using (var ws = new WebSocket("wss://yourserver.com/endpoint"))
            {
                ws.OnMessage += (sender, e) =>
                {
                    Console.WriteLine("Received: " + e.Data);
                };
    
                ws.Connect();
                ws.Send(signedXml);
    
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(true);
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.