我正在尝试使用 RestSharp 使端点正常工作,但未获得授权。我拥有所有 AWS 凭证,它创建了一个令牌,但是当通过 API 代码传递它时,我得到了未授权。它确实按预期生成了一个新的 toekn,但似乎有一些安全措施到位。仅供参考,我在 AWS 服务器内的租户中,所以我应该拥有特权。你能帮忙吗?我读到我需要一个用于客户端安全的 nuget 包,但不知道它的名称
public async Task WhenThePOSTCreateSomethingRequestIsInitiatedUsingAsync()
{
var accessId = "2591d7c7617f46bb90a19bedb9d34567";
var secretKeyId = "vXBx0PIVVV2TEgmdNNzptpE7WgfsQM0e6Epjc+1UJ0Y=";
var message = "GET" + "\n" + "https://gitlab.industrysoftware.automation.company.com/snippets/52/raw" + "\r\n\r\n" + "\r\n" + "application/json" + "\r\n" + DateTime.UtcNow.ToString("ddd, dd MMM yyy HH:mm:ss ") + "GMT" + "\r\n\r\n" + "dfh79867236hdfhgsdfg1111111" + "\n";
var authorization = CreateToken(message, secretKeyId);
var token = "S3 " + accessId + ":" + authorization;
var client1 = new
RestClient("https://sas.dev.aps.sws.company.com/sas/schedule/createSomething");
client1.Timeout = -1;
var request1 = new RestRequest(Method.POST);
request1.AddHeader("Accept", "*/*");
request1.AddHeader("Content-MD5", "");
request1.AddHeader("Content-Type", "application/json");
request1.AddHeader("X-Company-Authorization", token);
request1.AddHeader("Date", DateTime.UtcNow.ToString());
request1.AddHeader("X-Company-Correlation-Id", "DSS-fe09700654324aa1b4f1bac2dfb7ea63-48f5b253-599f-4ca7-8b2c-f627a9dd85b8");
var body = @"{
" + "\n" +
@" ""name"": ""Name""
" + "\n" +
@"}";
var data2 = JsonConvert.SerializeObject(body);
request1.AddParameter("application/json", data2, ParameterType.RequestBody);
request1.AddParameter("authorization", token);
request1.AddParameter("agentId", "sas-f6f9c108-6fa2-40ac-9671-cc757607a222");
var response1 = await client1.ExecuteAsync(request1);
}
private static string CreateToken(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secret);
Console.WriteLine(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
Console.WriteLine(messageBytes);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
byte[] keyBytes = System.Convert.FromBase64String(secret);
}