Exemplo n.º 1
0
        public static string genCredential(Dictionary <string, object> values)
        {
            Credential cred = new Credential {
                SecretId  = (string)values["secretId"],
                SecretKey = (string)values["secretKey"]
            };

            ClientProfile clientProfile = new ClientProfile();
            HttpProfile   httpProfile   = new HttpProfile();

            httpProfile.Endpoint      = ("sts.tencentcloudapi.com");
            clientProfile.HttpProfile = httpProfile;

            string region      = (string)values["region"];
            string bucket      = (string)values["bucket"];
            string allowPrefix = (string)values["allowPrefix"];

            string[] allowActions = (string[])values["allowActions"];
            string   policy       = getPolicy(region, bucket, allowPrefix, allowActions);

            Dictionary <string, object> body = new Dictionary <string, object>();

            body.Add("DurationSeconds", (Int32)values["durationSeconds"]);
            body.Add("Name", "cos-sts-sdk-dotnet");
            body.Add("Policy", policy);

            StsClient client = new StsClient(cred, region, clientProfile);
            GetFederationTokenRequest req = new GetFederationTokenRequest();

            string strParams = JsonConvert.SerializeObject(body);

            req = GetFederationTokenRequest.FromJsonString <GetFederationTokenRequest>(strParams);
            GetFederationTokenResponse resp = client.GetFederationToken(req).
                                              ConfigureAwait(false).GetAwaiter().GetResult();

            return(JsonConvert.SerializeObject(resp));
        }
Exemplo n.º 2
0
        public static Dictionary <string, object> genCredential(Dictionary <string, object> values)
        {
            checkArguments(values, new string[] { "secretId", "secretKey", "region" });

            Credential cred = new Credential {
                SecretId  = (string)values["secretId"],
                SecretKey = (string)values["secretKey"]
            };
            string region = (string)values["region"];

            ClientProfile clientProfile = new ClientProfile();
            HttpProfile   httpProfile   = new HttpProfile();
            String        endpoint      = values.ContainsKey("Domain") ? (string)values["Domain"]:
                                          "sts.tencentcloudapi.com";

            httpProfile.Endpoint      = endpoint;
            clientProfile.HttpProfile = httpProfile;

            // get policy
            string policy = null;

            if (values.ContainsKey("policy"))
            {
                policy = (string)values["policy"];
            }
            if (policy == null)
            {
                checkArguments(values, new string[] { "bucket", "allowActions" });
                string   bucket       = (string)values["bucket"];
                string[] allowActions = (string[])values["allowActions"];
                string[] allowPrefixes;
                if (values.ContainsKey("allowPrefix"))
                {
                    allowPrefixes = new string[] { (string)values["allowPrefix"] };
                }
                else if (values.ContainsKey("allowPrefixes"))
                {
                    allowPrefixes = (string[])values["allowPrefixes"];
                }
                else
                {
                    throw new System.ArgumentException("allowPrefix and allowPrefixes are both null.");
                }
                policy = getPolicy(region, bucket, allowPrefixes, allowActions);
            }

            // duration
            Int32 durationSeconds = 1800;

            if (values.ContainsKey("durationSeconds"))
            {
                durationSeconds = (Int32)values["durationSeconds"];
            }

            Dictionary <string, object> body = new Dictionary <string, object>();

            body.Add("DurationSeconds", durationSeconds);
            body.Add("Name", "cos-sts-sdk-dotnet");
            body.Add("Policy", policy);

            StsClient client = new StsClient(cred, region, clientProfile);
            GetFederationTokenRequest req = new GetFederationTokenRequest();

            string strParams = JsonConvert.SerializeObject(body);

            req = GetFederationTokenRequest.FromJsonString <GetFederationTokenRequest>(strParams);
            GetFederationTokenResponse resp = client.GetFederationToken(req).
                                              ConfigureAwait(false).GetAwaiter().GetResult();
            string jsonString = JsonConvert.SerializeObject(resp);
            Dictionary <string, object> dic = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonString);

            if (dic.ContainsKey("ExpiredTime"))
            {
                dic.Add("StartTime", Int32.Parse(dic["ExpiredTime"].ToString()) - durationSeconds);
            }
            return(dic);
        }