Samples

Last Updated: Jul 03, 2017

  1. using System;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Profile;
  4. using Aliyun.Acs.Sts.Model.V20150401;
  5. namespace sts_net_sdk_sample
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. // Construct an Aliyun Client for initiating a request
  12. // Set AccessKeyID and AccessKeySevcret when constructing the Aliyun Client
  13. // STS is a Global Service, and its API ingress is located in the East China 1 (Hangzhou) region. Enter "cn-hangzhou" in "Region"
  14. IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<access-key-id>", "<access-key-secret>");
  15. DefaultAcsClient client = new DefaultAcsClient(profile);
  16. // Construct an "AssumeRole" request
  17. AssumeRoleRequest request = new AssumeRoleRequest();
  18. // Specify the role Arn
  19. request.RoleArn = "<role-arn>";
  20. request.RoleSessionName = "<role-session-name>";
  21. // Set the Token's period of validity. It is an optional parameter with a default value of 3,600s.
  22. // request.DurationSeconds = 3600;
  23. // Set an additional policy for the Token so as to further limit the Token permission when obtaining the token.
  24. // request.Policy="<policy-content>"
  25. try
  26. {
  27. AssumeRoleResponse response = client.GetAcsResponse(request);
  28. Console.WriteLine("AccessKeyId: " + response.Credentials.AccessKeyId);
  29. Console.WriteLine("AccessKeySecret: " + response.Credentials.AccessKeySecret);
  30. Console.WriteLine("SecurityToken: " + response.Credentials.SecurityToken);
  31. //Token expiration time, and time for the server to return UTC, which is displayed in Beijing Time.
  32. Console.WriteLine("Expiration: " + DateTime.Parse(response.Credentials.Expiration).ToLocalTime());
  33. }
  34. catch (Exception ex)
  35. {
  36. Console.Write(ex.ToString());
  37. }
  38. Console.ReadLine();
  39. }
  40. }
  41. }
Thank you! We've received your feedback.