我正在嘗試爲Xamarin組件創建SimpleDB持久性框架。我無法使用.NET SDK,因爲並非所有必需的組件都存在於PCL項目中。出於這個原因,我試圖自己創建REST請求,但在嘗試發佈我的請求時,我不斷收到SignatureDoesNotMatch。AWS SimpleDB - SignatureDoesNotMatch - 用於Xamarin應用程序的PCL
有人可以幫忙,看看我的簽名是否有問題。
下面是要簽名
這裏是字符串被簽名
請求POST \ n sdb.amazonaws.com \ n /\ n & AWSAccessKeyId = AAAAAAAAAAAAAAAAAAAA & Action = PutAttributes & Attribute.1.Name = test & Attribute.1.Value = test & DomainName = test & ITEMNAME = 0 &是SignatureMethod = HmacSHA256 & SignatureVersion = 2 &時間戳= 2014-04-20T11%3A03 3A12%2B02%%3A00 &版本= 2009-04-15
這裏是簽名的請求。
測試AWSAccessKeyId = AAAAAAAAAAAAAAAAAAAA 測試AWSSecretKey = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
我已經包括了我的散列的C#文件,並要求製造商
public class PutAttributesRequestMarshaller
{
private const string ACTION = "PutAttributes";
private const string METHOD = "POST";
private const string SIGNATURE_METHOD = "HmacSHA256";
private const string SIGNATURE_VERSION = "2";
private const string VERSION = "2009-04-15";
private SortedDictionary<string, string> AttributesDic = new SortedDictionary<string, string>();
private PutAttributesRequest request;
public PutAttributesRequestMarshaller (PutAttributesRequest request)
{
this.request = request;
for (int i = 0; i < request.Attributes.Count; i++) {
ReplaceableAttribute Attribute = request.Attributes [i];
AttributesDic.Add ("Attribute." + (i + 1) + ".Name", Attribute.Name);
AttributesDic.Add ("Attribute." + (i + 1) + ".Value", Attribute.Value);
if (Attribute.Replace.Value) {
AttributesDic.Add ("Attribute." + (i + 1) + ".Replace", Attribute.Replace.ToString().ToLower());
}
}
}
public String Marshal()
{
StringBuilder sb = new StringBuilder();
sb.Append ("https://" + Region + "/");
sb.Append ("?Action=" + ACTION);
sb.Append ("&DomainName=" + request.DomainName);
sb.Append ("&ItemName=" + request.ItemName);
sb.Append (Attributes);
sb.Append ("&Version=" + VERSION);
sb.Append ("&Timestamp=" + Timestamp);
string signature = GenerateSignature();
string encoded = System.Net.WebUtility.UrlEncode (signature);
sb.Append ("&Signature=" + encoded);
sb.Append ("&SignatureVersion=" + SIGNATURE_VERSION);
sb.Append ("&SignatureMethod=" + SIGNATURE_METHOD);
sb.Append ("&AWSAccessKeyId=" + AWSAccessKeyId);
return sb.ToString();
}
private string Attributes {
get {
StringBuilder sb = new StringBuilder();
var enumerator = AttributesDic.GetEnumerator();
while (enumerator.MoveNext()) {
var entry = enumerator.Current;
sb.Append ("&");
sb.Append (System.Net.WebUtility.UrlEncode (entry.Key));
sb.Append ("=");
sb.Append (System.Net.WebUtility.UrlEncode (entry.Value));
}
return sb.ToString();
}
}
private string AWSAccessKeyId {
get {
return ServiceContainer.Resolve<SimpleDBClient>().AWSAccessKeyId;
}
}
private string AWSSecretKey {
get {
return ServiceContainer.Resolve<SimpleDBClient>().AWSSecretKey;
}
}
private string Timestamp {
get {
DateTime withOutMili = DateTime.Now;
string formatted = withOutMili.ToString ("yyyy-MM-ddTHH:mm:sszzzzz");
string encoded = System.Net.WebUtility.UrlEncode (formatted);
return encoded;
}
}
public string Region {
get {
return ServiceContainer.Resolve<SimpleDBClient>().Region;
}
}
private string GenerateSignature()
{
StringBuilder sb = new StringBuilder();
sb.Append (METHOD + "\n");
sb.Append (Region + "\n");
sb.Append ("/\n");
sb.Append ("&AWSAccessKeyId=" + AWSAccessKeyId);
sb.Append ("&Action=" + ACTION);
sb.Append (Attributes);
sb.Append ("&DomainName=" + request.DomainName);
sb.Append ("&ItemName=" + request.ItemName);
sb.Append ("&SignatureMethod=" + SIGNATURE_METHOD);
sb.Append ("&SignatureVersion=" + SIGNATURE_VERSION);
sb.Append ("&Timestamp=" + Timestamp);
sb.Append ("&Version=" + VERSION);
string signature = sb.ToString();
ISHA256Service service = ServiceContainer.Resolve<ISHA256Service>();
string hashed = service.CreateHash (signature, AWSSecretKey);
return hashed;
}
private SimpleDBClient Client {
get {
return ServiceContainer.Resolve<SimpleDBClient>();
}
}
public string CreateHash (string message, string secret)
{
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes (secret);
byte[] messageBytes = encoding.GetBytes (message);
using (var hmacsha256 = new HMACSHA256 (keyByte)) {
byte[] hashmessage = hmacsha256.ComputeHash (messageBytes);
return Convert.ToBase64String (hashmessage);
}
}
預先感謝您。