HI Everyone i am trying to authenticate a webservice which is hosted on a Apache server and uses AWS Authentication, i can get the first service running which gets the Access Id and Access Key, the second step is to sign the URL with the access Key we received from the step 1 and then send the second request with Authorization Header consisting AWS {AcessId}:{SignedSignature}
The Above method can be found out at http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
Now my Actual .net code is as follows
string datatoencrypt = "GET"+Environment.NewLine + Environment.NewLine + Environment.NewLine + isoDate + Environment.NewLine+ "alarms?updatedSince=20170501T2359Z&timeout=30";
GetSecondAPI(sessionInfo.accessKey, sessionInfo.id, datatoencrypt);
public static string Encode(string input, byte[] key)
{
HMACSHA1 myhmacsha1 = new HMACSHA1(key);
byte[] byteArray = Encoding.UTF8.GetBytes(input);
MemoryStream stream = new MemoryStream(byteArray);
return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
//return myhmacsha1.ComputeHash(byteArray);
}
public static byte[] ConvertFromStringToHex(string inputHex)
{
inputHex = inputHex.Replace("-", "");
byte[] resultantArray = new byte[inputHex.Length / 2];
for (int i = 0; i < resultantArray.Length; i++)
{
resultantArray[i] = Convert.ToByte(inputHex.Substring(i * 2, 2), 16);
}
return resultantArray;
}
private async void GetSecondAPI(string accessKey, string id, string dataencry)
{
byte[] key = Encoding.UTF8.GetBytes(accessKey);
var details = Encode(dataencry, key);
byte[] data = ConvertFromStringToHex(details);
string base64 = Convert.ToBase64String(data);
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://10.25.11.125:443/api/v1/"); // this is local server so cant test it outside the network
client.DefaultRequestHeaders.Date = DateTime.Now;
string authorization = string.Format("AWS {0}:{1}", id, base64);
client.DefaultRequestHeaders.Add("Authorization","AWS " + id + ":" + base64);
ServicePointManager.ServerCertificateValidationCallback = delegate (
Object obj, X509Certificate certificate, X509Chain chain,
SslPolicyErrors errors)
{
return (true);
};
var response = await client.GetAsync(callURL);
var result = response.Content.ReadAsStringAsync().Result;
}
}
This code runs perfectly but it gives an Unauthorized 401 error, i guess the part which is giving issues is the datatoencrypt as i am following the way AWS has mentioned in their document and second issue i guess could be that .net is adding some headers which is failing the authorization at the AWS side.
Any help is really appreciated