Signing URL – C#

This example illustrates how to sign request URL’s using .NET C#. To use this sample you need the latest version of the .NET framework.

Definition

Name Type Summary
domain string The protocol + domain of the web service you are calling. ex. http://uk.shopping.yahooapis.com
path string The path + query string of the web service you are calling. ex. /V2/search?query=ipod
id string Your authentication ID provided to you by Kelkoo.
key string Your authentication key provided to you by Kelkoo.

Don’t forget to encode the values in your query string. In .NET C# you can use Server.UrlEncode(string url).

Sample Class

Top level Elements

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace Kelkoo
{

public static class RequestSigner
{

public static string Sign(string domain, string path, string id, string key)
{
/// Get Timestamp
int timestamp = (int)(((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds));

// Clean up path
path = path.Replace(" ", "+");

// Get url
string url = path + "&aid=" + auth_id + "&timestamp=" + timestamp;

// Get url+key byte array
byte[] tokken = Encoding.GetEncoding("iso-8859-1").GetBytes(url + auth_key);

// Get MD5 instance
MD5 md5 = MD5CryptoServiceProvider.Create();

// Compute MD5 hash
byte[] md5tokken = md5.ComputeHash(tokken);

//Convert to base 64 string
string base64tokken = Convert.ToBase64String(md5tokken);

// Clean up tokken
base64tokken = base64tokken.Replace("+", ".").Replace("/", "_").Replace("=", "-");

// return signed url;
return domain + url + "&hash=" + base64tokken;

}

}

}

Sample

Call: RequestSigner.Sign("http://fr.shopping.yahooapis.com", "/V2/search?query=sony","96912377", "KeyOfPartner96912377");

Output: 'http://fr.shopping.yahooapis.com/V2/search?query=sony&aid=96912377&timestamp=1166428730&hash=oVsgSMxjHws4UVgPzxklmw--'

Downloads

Download a full sample class: