Exemplo n.º 1
0
        public override HttpRequest SignRequest(Signer signer, AlibabaCloudCredentials credentials,
                                                FormatType?format, ProductDomain domain)
        {
            if (this.BodyParameters != null && this.BodyParameters.Count > 0)
            {
                Dictionary <String, String> formParams = new Dictionary <String, String>(this.BodyParameters);
                string formStr  = ConcatQueryString(formParams);
                byte[] formData = System.Text.Encoding.UTF8.GetBytes(formStr);
                this.SetContent(formData, "UTF-8", FormatType.FORM);
            }

            Dictionary <string, string> imutableMap = new Dictionary <string, string>(this.Headers);

            if (null != signer && null != credentials)
            {
                String accessKeyId = credentials.GetAccessKeyId();
                imutableMap = Composer.RefreshSignParameters(Headers, signer, accessKeyId, format);
                if (credentials is BasicSessionCredentials)
                {
                    String sessionToken = ((BasicSessionCredentials)credentials).GetSessionToken();
                    if (null != sessionToken)
                    {
                        imutableMap.Add("x-acs-security-token", sessionToken);
                    }
                }

                String strToSign = Composer.ComposeStringToSign(Method, uriPattern, signer,
                                                                QueryParameters, imutableMap, pathParameters);
                String signature = signer.SignString(strToSign, credentials);
                DictionaryUtil.Add(imutableMap, "Authorization", "acs " + accessKeyId + ":" + signature);
            }
            Url          = this.ComposeUrl(domain.DomianName, QueryParameters);
            this.Headers = imutableMap;
            return(this);
        }
        public override HttpRequest SignRequest(Signer signer, AlibabaCloudCredentials credentials,
                                                FormatType?format, ProductDomain domain)
        {
            if (BodyParameters != null && BodyParameters.Count > 0)
            {
                var    formParams = new Dictionary <string, string>(this.BodyParameters);
                string formStr    = ConcatQueryString(formParams);
                byte[] formData   = System.Text.Encoding.UTF8.GetBytes(formStr);
                SetContent(formData, "UTF-8", FormatType.FORM);
            }

            var imutableMap = new Dictionary <string, string>(this.Headers);

            if (null != signer && null != credentials)
            {
                var accessKeyId = credentials.GetAccessKeyId();
                imutableMap = Composer.RefreshSignParameters(Headers, signer, accessKeyId, format);
                switch (credentials)
                {
                case BasicSessionCredentials sessionCredentials:
                {
                    var sessionToken = sessionCredentials.GetSessionToken();
                    if (null != sessionToken)
                    {
                        imutableMap.Add("x-acs-security-token", sessionToken);
                    }

                    break;
                }

                case BearerTokenCredential credential:
                {
                    var bearerToken = credential.GetBearerToken();
                    if (null != bearerToken)
                    {
                        QueryParameters.Add("x-acs-bearer-token", bearerToken);
                    }

                    break;
                }
                }

                var strToSign = Composer.ComposeStringToSign(Method, UriPattern, signer,
                                                             QueryParameters, imutableMap, PathParameters);
                var signature = signer.SignString(strToSign, credentials);
                DictionaryUtil.Add(imutableMap, "Authorization", "acs " + accessKeyId + ":" + signature);
            }
            Url     = ComposeUrl(domain.DomianName, QueryParameters);
            Headers = imutableMap;
            return(this);
        }
        public override HttpRequest SignRequest(Signer signer, AlibabaCloudCredentials credentials,
                                                FormatType?format, ProductDomain domain)
        {
            var imutableMap = new Dictionary <string, string>(QueryParameters);

            if (null != signer && null != credentials)
            {
                var accessKeyId  = credentials.GetAccessKeyId();
                var accessSecret = credentials.GetAccessKeySecret();

                var sessionCredentials = credentials as BasicSessionCredentials;
                var sessionToken       = sessionCredentials == null ? null : sessionCredentials.GetSessionToken();
                if (sessionToken != null)
                {
                    DictionaryUtil.Add(QueryParameters, "SecurityToken", sessionToken);
                }

                var credential  = credentials as BearerTokenCredential;
                var bearerToken = credential == null ? null : credential.GetBearerToken();
                if (bearerToken != null)
                {
                    DictionaryUtil.Add(QueryParameters, "BearerToken", bearerToken);
                }

                imutableMap = Composer.RefreshSignParameters(QueryParameters, signer, accessKeyId, format);
                DictionaryUtil.Add(imutableMap, "RegionId", RegionId);

                var paramsToSign = new Dictionary <string, string>(imutableMap);
                if (BodyParameters != null && BodyParameters.Count > 0)
                {
                    var formParams = new Dictionary <string, string>(BodyParameters);
                    var formStr    = ConcatQueryString(formParams);
                    var formData   = System.Text.Encoding.UTF8.GetBytes(formStr);
                    SetContent(formData, "UTF-8", FormatType.FORM);
                    foreach (var formParam in formParams)
                    {
                        DictionaryUtil.Add(paramsToSign, formParam.Key, formParam.Value);
                    }
                }

                var strToSign = Composer.ComposeStringToSign(Method, null, signer, paramsToSign, null, null);
                var signature = signer.SignString(strToSign, accessSecret + "&");
                DictionaryUtil.Add(imutableMap, "Signature", signature);

                StringToSign = strToSign;
            }

            Url = ComposeUrl(domain.DomainName, imutableMap);
            return(this);
        }
Exemplo n.º 4
0
        public override HttpRequest SignRequest(ISigner signer, Credential credential, FormatType?format, ProductDomain domain)
        {
            var imutableMap = new Dictionary <string, string>(Headers);

            if (null != signer && null != credential)
            {
                var accessKeyId  = credential.AccessKeyId;
                var accessSecret = credential.AccessSecret;
                imutableMap = Composer.RefreshSignParameters(Headers, signer, accessKeyId, format);
                var strToSign = Composer.ComposeStringToSign(Method, UriPattern, signer, QueryParameters, imutableMap, PathParameters);
                var signature = signer.SignString(strToSign, accessSecret);
                DictionaryUtil.Add(imutableMap, "Authorization", $"acs {accessKeyId}:{signature}");
            }
            Url     = ComposeUrl(domain.DomianName, QueryParameters);
            Headers = imutableMap;
            return(this);
        }
        public override HttpRequest SignRequest(ISigner signer, Credential credential, FormatType?format, ProductDomain domain)
        {
            var imutableMap = new Dictionary <string, string>(QueryParameters);

            if (null != signer && null != credential)
            {
                var accessKeyId  = credential.AccessKeyId;
                var accessSecret = credential.AccessSecret;
                imutableMap = Composer.RefreshSignParameters(QueryParameters, signer, accessKeyId, format);
                imutableMap.Add("RegionId", RegionId);
                var strToSign = Composer.ComposeStringToSign(Method, null, signer, imutableMap, null, null);
                var signature = signer.SignString(strToSign, accessSecret + "&");
                imutableMap.Add("Signature", signature);
            }
            var request = new HttpRequest(ComposeUrl(domain.DomianName, imutableMap))
            {
                Method = Method
            };

            return(request);
        }