Mohammad Al-Daqqah
Mohammad Al-Daqqah

Reputation: 11

How do I add security Headers to my SOAP Service

I'm trying to call a WCF service from a WSDL given to me by the 3rd party which I'm trying to integrate with, they gave me a sample request that my code should generate, it contains wsse:Security element and it has two digest and a signature see below (I have removed the namespaces from the soapenv:Envelope):

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:ns3="" xmlns:ns2="" xmlns:ns1="" xmlns:ns="" xmlns:soapenv="">
    <soapenv:Header>
        <ns:MessageHeader>
            <ns:ServiceInitiatorKey>801</ns:ServiceInitiatorKey>
            <ns:ServiceProviderKey>001</ns:ServiceProviderKey>
            <ns:ServiceConsumerId>801</ns:ServiceConsumerId>
            <ns:RqUID>22xzo5bi-8e98-8bnn-32ge-xz3a6eiss241</ns:RqUID>
            <ns:Date>2021-11-09T17:17:10</ns:Date>
            <ns:Lang>en-gb</ns:Lang>
        </ns:MessageHeader>
        <wsse:Security xmlns:wsse="" soapenv:mustUnderstand="1">
            <wsu:Timestamp wsu:Id="Timestamp-244f9b6d-a6a6-4909-8143-ba1aefb6e8dd" xmlns:wsu="">
                <wsu:Created>2022-04-05T13:46:19Z</wsu:Created>
                <wsu:Expires>2022-04-05T13:51:19Z</wsu:Expires>
            </wsu:Timestamp>
            <wsse:BinarySecurityToken wsu:Id="SecurityToken-493e1d98-bc0a-4e80-b5ca-ba1aefb6d910" EncodingType="" ValueType="" xmlns:wsu="">base64 certificate</wsse:BinarySecurityToken>
            <Signature xmlns="">
                <SignedInfo>
                    <CanonicalizationMethod Algorithm=""/>
                    <SignatureMethod Algorithm=""/>
                    <Reference URI="#Timestamp-244f9b6d-a6a6-4909-8143-ba1aefb6e8dd">
                        <Transforms>
                            <Transform Algorithm=""/>
                        </Transforms>
                        <DigestMethod Algorithm=""/>
                        <DigestValue>S8Hlmk9nw1a0s5l+Q98ZH+kJycQ=</DigestValue>
                    </Reference>
                    <Reference URI="#Body-95ba4897-1060-42c4-ac8d-ba1aefb68631">
                        <Transforms>
                            <Transform Algorithm=""/>
                        </Transforms>
                        <DigestMethod Algorithm=""/>
                        <DigestValue>5VIb9TVR12MpmcXakp5gGJTTZYc=</DigestValue>
                    </Reference>
                </SignedInfo>
                <SignatureValue>UVvKd3snupusij/MDYbykmlWe+/tm+gQIvJ9aCav2nwpvKM4AwPrxmjcp3Nvo6s8UDeBsxlk/6E1FtCt1FWhJEwUewIpmCsK1Urp2xf0W74FMDfz4ABB7xpr6S/loGwznOlFPz59Ih1gP+AYVo8D0WnaFvjycm4DH97YmcDs2j4R3YyYJA/IjiJ2QPS4O+f7Ne+/cAbz19FQRUfQVTLu7KQrQAs7adjj4vvJuVgbqKZo45fIL1/HSYt+bXXOYe+PfOCPs6UBy0SUlZy/OEucz7BwldhN921ReuQmhPdPKUtDZ8MGr4T72pep260Pu7hmDtUspxTLzt3J8OUyMGtcxQ==</SignatureValue>
                <KeyInfo>
                    <wsse:SecurityTokenReference xmlns="">
                        <wsse:Reference URI="#SecurityToken-493e1d98-bc0a-4e80-b5ca-ba1aefb6d910" ValueType=""/>
                    </wsse:SecurityTokenReference>
                </KeyInfo>
            </Signature>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body wsu:Id="Body-95ba4897-1060-42c4-ac8d-ba1aefb68631" xmlns:wsu="">
        <ns1:LoadRq>
            <ns1:Timestamp>2021-11-09T17:17:10</ns1:Timestamp>
            <ns1:Payment>
                <ns1:BillerId>10002</ns1:BillerId>
                <ns1:CurAmt>200</ns1:CurAmt>
                <ns1:PrcDt>2021-11-09T17:17:10</ns1:PrcDt>
                <ns1:DueDt>2022-12-09T17:17:10</ns1:DueDt>
                <ns1:BillerPmtId>1000256897</ns1:BillerPmtId>
                <ns1:PaymentRef>
                    <ns3:BillNumberWithAccount>
                        <ns3:BillingAcct>10002261195</ns3:BillingAcct>
                        <ns3:BillNumber>10002261195</ns3:BillNumber>
                    </ns3:BillNumberWithAccount>         
                </ns1:PaymentRef>
            </ns1:Payment>
        </ns1:LoadRq>
    </soapenv:Body>
</soapenv:Envelope>

I'm using a client form the WSDL they provided and my code generates an XML envelope as below (it's a different service from the one above but it should have the same security headers, also I have removed the namespaces of some elements):

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="">
    <s:Header>
        <Action s:mustUnderstand="1" xmlns=""></Action>
        <h:MessageHeader xmlns="" xmlns:xsi="" xmlns:xsd="" xmlns:h="">
            <ServiceInitiatorKey>908</ServiceInitiatorKey>
            <ServiceProviderKey>001</ServiceProviderKey>
            <ServiceConsumerId>908</ServiceConsumerId>
            <RqUID>de9f7eea-c84f-4b92-8541-fceaab54260c</RqUID>
            <Date>2024-12-12T14:54:28</Date>
            <Lang>en-gb</Lang>
        </h:MessageHeader>
    </s:Header>
    <s:Body xmlns:xsi="" xmlns:xsd="">
        <CreateRq xmlns="">
            <Partner>
                <PartnerInfo xmlns="">
                    <PartnerType>SUB-BILLER</PartnerType>
                    <OwnerId>908</OwnerId>
                </PartnerInfo>
            </Partner>
        </CreateRq>
    </s:Body>
</s:Envelope>

how can I add the needed security headers and how do I calculate them and what do they even mean? the client has a function that accept two objects, the MessageHeader and the Partner and I'm using _client.CreateAsync(MessageHeader, Partner);

can someone please help me? I have been looking for a solution for more than a week now and I can't seem to understand it, I'm using .net core 8

Many thanks.

I have tried to add a messageInspector and modify the XML beforeSend but with no luck, and other threads here seem to add username and password and that's not what i'm trying to achieve.

Upvotes: 1

Views: 30

Answers (0)

Related Questions