コード例 #1
0
        public void ComposeUrlTest()
        {
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("key", "value");
            dic.Add("keyNull", null);
            Assert.NotNull(ParameterHelper.ComposeUrl("www.aliyun.com", dic, "http"));
        }
コード例 #2
0
        private async Task <IAlibabaCloudCredentials> GetNewSessionCredentialsAsync(IConnClient client)
        {
            HttpRequest httpRequest = new HttpRequest();

            httpRequest.SetCommonUrlParameters();
            httpRequest.AddUrlParameter("Action", "AssumeRole");
            httpRequest.AddUrlParameter("Format", "JSON");
            httpRequest.AddUrlParameter("Version", "2015-04-01");
            httpRequest.AddUrlParameter("DurationSeconds", durationSeconds.ToString());
            httpRequest.AddUrlParameter("RoleArn", this.roleArn);
            httpRequest.AddUrlParameter("AccessKeyId", this.accessKeyId);
            httpRequest.AddUrlParameter("RegionId", this.regionId);
            httpRequest.AddUrlParameter("RoleSessionName", this.roleSessionName);
            if (policy != null)
            {
                httpRequest.AddUrlParameter("Policy", this.policy);
            }

            httpRequest.Method         = MethodType.Get;
            httpRequest.ConnectTimeout = connectTimeout;
            httpRequest.ReadTimeout    = readTimeout;
            string strToSign = ParameterHelper.ComposeStringToSign(MethodType.Get, httpRequest.UrlParameters);
            string signature = ParameterHelper.SignString(strToSign, accessKeySecret + "&");

            httpRequest.AddUrlParameter("Signature", signature);
            httpRequest.Url = ParameterHelper.ComposeUrl("sts.aliyuncs.com", httpRequest.UrlParameters,
                                                         "https");
            HttpResponse httpResponse = await client.DoActionAsync(httpRequest);

            Dictionary <string, object> map =
                JsonConvert.DeserializeObject <Dictionary <string, object> >(httpResponse.GetHttpContentString());

            if (map.ContainsKey("Credentials"))
            {
                string credentialsJson = JsonConvert.SerializeObject(DictionaryUtil.Get(map, "Credentials"));
                Dictionary <string, string> credentials =
                    JsonConvert.DeserializeObject <Dictionary <string, string> >(credentialsJson);
                string expirationStr =
                    DictionaryUtil.Get(credentials, "Expiration").Replace('T', ' ').Replace('Z', ' ');
                var  dt         = Convert.ToDateTime(expirationStr);
                long expiration = dt.GetTimeMillis();
                accessKeyId     = DictionaryUtil.Get(credentials, "AccessKeyId");
                accessKeySecret = DictionaryUtil.Get(credentials, "AccessKeySecret");
                securityToken   = DictionaryUtil.Get(credentials, "SecurityToken");
                return(new RamRoleArnCredential(accessKeyId, accessKeySecret, securityToken, expiration, this));
            }

            throw new CredentialException(JsonConvert.SerializeObject(map));
        }
コード例 #3
0
        private IAlibabaCloudCredentials GetNewSessionCredentials(IConnClient client)
        {
            HttpRequest httpRequest = new HttpRequest();

            httpRequest.SetCommonUrlParameters();
            httpRequest.AddUrlParameter("Action", "GenerateSessionAccessKey");
            httpRequest.AddUrlParameter("Format", "JSON");
            httpRequest.AddUrlParameter("Version", "2015-04-01");
            httpRequest.AddUrlParameter("DurationSeconds", durationSeconds.ToString());
            httpRequest.AddUrlParameter("AccessKeyId", PublicKeyId);
            httpRequest.AddUrlParameter("RegionId", regionId);
            string strToSign = ParameterHelper.ComposeStringToSign(MethodType.Get, httpRequest.UrlParameters);
            String signature = ParameterHelper.SignString(strToSign, PrivateKey + "&");

            httpRequest.AddUrlParameter("Signature", signature);
            httpRequest.Method         = MethodType.Get;
            httpRequest.ConnectTimeout = connectTimeout;
            httpRequest.ReadTimeout    = readTimeout;
            httpRequest.Url            = ParameterHelper.ComposeUrl("sts.aliyuncs.com", httpRequest.UrlParameters, "https");
            HttpResponse httpResponse = client.DoAction(httpRequest);

            if (httpResponse != null && httpResponse.Status != 200)
            {
                throw new CredentialException("Failed to get session credentials.HttpCode=" + httpResponse.Status);
            }

            Debug.Assert(httpResponse != null, "httpResponse != null");
            dynamic contentObj = JsonConvert.DeserializeObject <dynamic>(httpResponse.GetHttpContentString());
            string  sessionAccessKeyId;
            string  sessionAccessKeySecret;
            string  expirationStr;

            try
            {
                sessionAccessKeyId     = contentObj.SessionAccessKey.SessionAccessKeyId;
                sessionAccessKeySecret = contentObj.SessionAccessKey.SessionAccessKeySecret;
                expirationStr          = contentObj.SessionAccessKey.Expiration;
            }
            catch
            {
                throw new CredentialException("Invalid json got from service.");
            }
            expirationStr = expirationStr.Replace('T', ' ').Replace('Z', ' ');
            var  dt         = Convert.ToDateTime(expirationStr);
            long expiration = dt.GetTimeMillis();

            return(new RsaKeyPairCredential(sessionAccessKeyId, sessionAccessKeySecret, expiration, this));
        }