コード例 #1
0
        public void Authenticate(IRestClient client, IRestRequest request)
        {
            var body              = request.Parameters.First(p => p.Type == ParameterType.RequestBody).Value.ToString();
            var contentHash       = Aws4SignerBase.CanonicalRequestHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(body));
            var contentHashString = Aws4SignerBase.ToHexString(contentHash, true);

            var headers = new Dictionary <string, string>
            {
                { Aws4SignerBase.X_Amz_Content_SHA256, contentHashString },
                { "content-type", "application/json" }
            };

            var signer = new Aws4SignerForAuthorizationHeader
            {
                EndpointUri = new Uri(client.BaseUrl + request.Resource),
                HttpMethod  = request.Method.ToString(),
                Service     = "es",
                Region      = Aws4SignerRegion
            };

            var authorizationHeaderValue = signer.ComputeSignature(headers,
                                                                   "", // no query parameters
                                                                   contentHashString,
                                                                   Aws4SignerAccessKey,
                                                                   Aws4SignerSecretKey);

            foreach (var header in headers.Keys)
            {
                if (header.Equals("host", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (header.Equals("content-length", StringComparison.OrdinalIgnoreCase))
                {
                    request.AddHeader("content-length", long.Parse(headers[header]).ToString());
                }
                else if (header.Equals("content-type", StringComparison.OrdinalIgnoreCase))
                {
                    request.AddHeader("content-type", headers[header]);
                }
                else
                {
                    request.AddHeader(header, headers[header]);
                }
            }
            request.AddHeader("Authorization", authorizationHeaderValue);
        }
コード例 #2
0
        public string CreateAuthenticationHeader(RequestData requestData)
        {
            var webRequest        = requestData.WebRequest;
            var contentHash       = Aws4SignerBase.CanonicalRequestHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(requestData.RequestString));
            var contentHashString = Aws4SignerBase.ToHexString(contentHash, true);

            var headers = new Dictionary <string, string>
            {
                { Aws4SignerBase.X_Amz_Content_SHA256, contentHashString },
                { "content-type", "text/plain" }
            };

            var signer = new Aws4SignerForAuthorizationHeader
            {
                EndpointUri = new Uri(requestData.Url),
                HttpMethod  = webRequest.Method,
                Service     = "es",
                Region      = Aws4SignerRegion
            };

            var authorizationHeaderValue = signer.ComputeSignature(headers,
                                                                   "", // no query parameters
                                                                   contentHashString,
                                                                   Aws4SignerAccessKey,
                                                                   Aws4SignerSecretKey);

            foreach (var header in headers.Keys)
            {
                if (header.Equals("host", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (header.Equals("content-length", StringComparison.OrdinalIgnoreCase))
                {
                    webRequest.ContentLength = long.Parse(headers[header]);
                }
                else if (header.Equals("content-type", StringComparison.OrdinalIgnoreCase))
                {
                    webRequest.ContentType = headers[header];
                }
                else
                {
                    webRequest.Headers.Add(header, headers[header]);
                }
            }
            return(authorizationHeaderValue);
        }