title: How to sign an Amazon web service request in .NET with SOAP and without WSE
body: <p>The Amazon Product Advertising API (formerly Amazon Associates Web Service or Amazon AWS) has implemented a new rule which is by August 15th 2009 all web service requests to them must be signed. They have provided sample code on their site showing how to do this in C# using both REST and SOAP. The implementation that I’m using is SOAP. You can find the sample code <a href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=2481&amp;categoryID=14" rel="nofollow noreferrer">here</a>, I’m not including it because there is a fair amount.</p>
  
  <p>The problem I’m having is their sample code uses WSE 3 and our current code doesn’t use WSE. Does anyone know how to implement this update with just using the auto generated code from the WSDL? I’d like to not have to switch over to the WSE 3 stuff right now if I don’t have to since this update is more of a quick patch to hold us over until we can fully implement this in the current dev version (August 3rd they’re starting to drop 1 in 5 requests, in the live environment, if they aren’t signed which is bad news for our application).</p>
  
  <p>Here’s a snippet of the main portion that does the actual signing of the SOAP request.</p>
  
  <pre><code>class ClientOutputFilter : SoapFilter
  {
      // to store the AWS Access Key ID and corresponding Secret Key.
      String akid;
      String secret;
  
      // Constructor
      public ClientOutputFilter(String awsAccessKeyId, String awsSecretKey)
      {
          this.akid = awsAccessKeyId;
          this.secret = awsSecretKey;
      }
  
      // Here's the core logic:
      // 1. Concatenate operation name and timestamp to get StringToSign.
      // 2. Compute HMAC on StringToSign with Secret Key to get Signature.
      // 3. Add AWSAccessKeyId, Timestamp and Signature elements to the header.
      public override SoapFilterResult ProcessMessage(SoapEnvelope envelope)
      {
          var body = envelope.Body;
          var firstNode = body.ChildNodes.Item(0);
          String operation = firstNode.Name;
  
          DateTime currentTime = DateTime.UtcNow;
          String timestamp = currentTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
  
          String toSign = operation + timestamp;
          byte[] toSignBytes = Encoding.UTF8.GetBytes(toSign);
          byte[] secretBytes = Encoding.UTF8.GetBytes(secret);
          HMAC signer = new HMACSHA256(secretBytes);  // important! has to be HMAC-SHA-256, SHA-1 will not work.
  
          byte[] sigBytes = signer.ComputeHash(toSignBytes);
          String signature = Convert.ToBase64String(sigBytes); // important! has to be Base64 encoded
  
          var header = envelope.Header;
          XmlDocument doc = header.OwnerDocument;
  
          // create the elements - Namespace and Prefix are critical!
          XmlElement akidElement = doc.CreateElement(
              AmazonHmacAssertion.AWS_PFX, 
              "AWSAccessKeyId", 
              AmazonHmacAssertion.AWS_NS);
          akidElement.AppendChild(doc.CreateTextNode(akid));
  
          XmlElement tsElement = doc.CreateElement(
              AmazonHmacAssertion.AWS_PFX,
              "Timestamp",
              AmazonHmacAssertion.AWS_NS);
          tsElement.AppendChild(doc.CreateTextNode(timestamp));
  
          XmlElement sigElement = doc.CreateElement(
              AmazonHmacAssertion.AWS_PFX,
              "Signature",
              AmazonHmacAssertion.AWS_NS);
          sigElement.AppendChild(doc.CreateTextNode(signature));
  
          header.AppendChild(akidElement);
          header.AppendChild(tsElement);
          header.AppendChild(sigElement);
  
          // we're done
          return SoapFilterResult.Continue;
      }
  }
  </code></pre>
  
  <p>And that gets called like this when making the actual web service call</p>
  
  <pre><code>// create an instance of the serivce
  var api = new AWSECommerceService();
  
  // apply the security policy, which will add the require security elements to the
  // outgoing SOAP header
  var amazonHmacAssertion = new AmazonHmacAssertion(MY_AWS_ID, MY_AWS_SECRET);
  api.SetPolicy(amazonHmacAssertion.Policy());
  </code></pre>