/// <summary> /// 获取联合身份临时访问凭证 /// </summary> /// <returns></returns> public GetFederationTokenResponse GetFederationToken() { Credential cred = new Credential { SecretId = _cosConfig.SecretId, SecretKey = _cosConfig.SecretKey }; ClientProfile clientProfile = new ClientProfile(); HttpProfile httpProfile = new HttpProfile(); httpProfile.Endpoint = _cosConfig.EndPoint; clientProfile.HttpProfile = httpProfile; StsClient client = new StsClient(cred, _cosConfig.Region, clientProfile); GetFederationTokenRequest req = new GetFederationTokenRequest(); req.Name = _cosConfig.Name; req.Policy = HttpUtility.UrlEncode(_cosConfig.Policy); req.DurationSeconds = _cosConfig.DurationSeconds; GetFederationTokenResponse resp = client.GetFederationTokenSync(req); return(resp); }
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.GetFederationTokenSync(req); 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); }