SOAP Security Header

Adding SOAP Security Header in C#

Adding SOAP Security Header In C#

SOAP request must contain security header. The current version of the WSDL does not specify the header, thus the code for it is not present in classes generated by the wsdl.exe.

For Each Service You Must Declare The Security Class As Following:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.33440")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name = "PaymentServiceBinding",
  Namespace = "http://www.mygemini.com/schemas/mygemini")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof (AbstractIo))]
public partial class PaymentService: System.Web.Services.Protocols.SoapHttpClientProtocol {
    private System.Threading.SendOrPostCallback
    ImportSinglePaymentOrdersOperationCompleted;
    private System.Threading.SendOrPostCallback ImportBatchPaymentOrderOperationCompleted;
    private System.Threading.SendOrPostCallback GetPaymentOrderStatusOperationCompleted;
    // BEGIN manual addition for Security header
    [XmlRoot(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-secext - 1.0.xsd ")]
        public partial class Security: SoapHeader {
          public UsernameToken UsernameToken {
            get;
            set;
          }
        }
        public partial class UsernameToken {
          public string Username {
            get;
            set;
          }
          public string Password {
            get;
            set;
          }
          public string Nonce {
            get;
            set;
          }
        }
        public Security secHeader;
        // END manual addition for Security header

Then For Each Method You Have To Add The SoapHeader Decorator:

/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.mygemini.com/schemas/mygemini/ImportSinglePaymentOrders",
  Use = System.Web.Services.Description.SoapBindingUse.Literal,
  ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Bare)]
[
  return:
    System.Xml.Serialization.XmlArrayAttribute("ImportSinglePaymentOrdersResponseIo",
      Namespace = "http://www.mygemini.com/schemas/mygemini")
]
[
  return: System.Xml.Serialization.XmlArrayItemAttribute("PaymentOrdersResults",
    IsNullable = false)
]
[SoapHeader("secHeader", Direction = SoapHeaderDirection.In)]
public PaymentOrderResultIo[]
ImportSinglePaymentOrders([System.Xml.Serialization.XmlArrayAttribute(Namespace = "http://www.mygemini.com/schemas/mygemini")]
  [System.Xml.Serialization.XmlArrayItemAttribute("singlePaymentOrder", IsNullable = false)] PaymentOrderIo[] ImportSinglePaymentOrdersRequestIo) {
  object[] results = this.Invoke("ImportSinglePaymentOrders", new object[] {
    ImportSinglePaymentOrdersRequestIo
  });
  return ((PaymentOrderResultIo[])(results[0]));
}

Basically, you can declare the class anywhere in your project - just the instance must be visible for thed ecorator.

Finally, Sample Usage Of The ImportSinglePaymentOrders:

class Program {
  static void Main(string[] args) {
    var ps = new PaymentService();
    addSecurityHeader(ps);
    ps.Url = "https://test.tbconline.ge/dbi/dbiService";
    var paymentOrder = createSamplePaymentOrder();
    var paymentOrders = new TransferToOwnAccountPaymentOrderIo[1] {
      paymentOrder
    };
    var paymentOrderResult = ps.ImportSinglePaymentOrders(paymentOrders);
    System.Diagnostics.Debug.WriteLine("Resulting paymentId: {0}",
      paymentOrderResult[0].paymentId);
  }
  private static TransferToOwnAccountPaymentOrderIo createSamplePaymentOrder() {
    var paymentOrder = new TransferToOwnAccountPaymentOrderIo();
    paymentOrder.creditAccount = new AccountIdentificationIo();
    paymentOrder.creditAccount.accountNumber = "GE86TB1144836120100002";
    paymentOrder.creditAccount.accountCurrencyCode = "USD";
    paymentOrder.debitAccount = new AccountIdentificationIo();
    paymentOrder.debitAccount.accountNumber = "GE69TB1144836020100001";
    paymentOrder.debitAccount.accountCurrencyCode = "GEL";
    paymentOrder.amount = new MoneyIo();
    paymentOrder.amount.amount = 1;
    paymentOrder.amount.currency = "USD";
    paymentOrder.description = "my best description";
    return paymentOrder;
  }
  private static void addSecurityHeader(PaymentService ps) {
    ps.secHeader = new PaymentService.Security();
    ps.secHeader.UsernameToken = new PaymentService.UsernameToken();
    ps.secHeader.UsernameToken.Username = "USERNAME";
    ps.secHeader.UsernameToken.Password = "PASSWORD";
    ps.secHeader.UsernameToken.Nonce = "1111";
  }
}