This topic describes how to install .NET SDK and provides an operation example.

Installation

To download .NET SDK, click either of the following links:

Note STS is available on the NuGet platform. For more infomation, see STS NuGet.

Example

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Sts.Model.V20150401;
namespace StsNetSdkDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            const string REGIONID = "cn-shanghai";
            const string ENDPOINT = "sts.cn-shanghai.aliyuncs.com";
            // Construct an Alibaba Cloud client to initiate a request.
            // Set the access key ID and access key secret.
            DefaultProfile.AddEndpoint(REGIONID, REGIONID, "Sts", ENDPOINT);
            IClientProfile profile = DefaultProfile.GetProfile(REGIONID, "<access-key-id>", "<access-key-secret>");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            // Construct the "AssumeRole" request.
            AssumeRoleRequest request = new AssumeRoleRequest();
            request.AcceptFormat = FormatType.JSON;
            // Specify the Alibaba Cloud Resource Name (ARN) of the role.
            request.RoleArn = "<role-arn>";
            request.RoleSessionName = "<role-session-name>";
            // Optional. Set the validity period of the token. The default value is 3600, in seconds.
            // request.DurationSeconds = 3600;
            // Specify the token permission by attaching a policy to the token.
            // request.Policy="<policy-content>"
            try
            {
                AssumeRoleResponse response = client.GetAcsResponse(request);
                Console.WriteLine("AccessKeyId: " + response.Credentials.AccessKeyId);
                Console.WriteLine("AccessKeySecret: " + response.Credentials.AccessKeySecret);
                Console.WriteLine("SecurityToken: " + response.Credentials.SecurityToken);
                // The date and time when the token expires. The servers returns the UTC time by default. Here, change the time to UTC+8.
                Console.WriteLine("Expiration: " + DateTime.Parse(response.Credentials.Expiration).ToLocalTime());
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            Console.ReadLine();
        }
    }
}
Note