This topic describes how to install Security Token Service (STS) SDK for .NET and provides an example on how to use STS SDK for .NET.

Background information

  • To use STS SDK for .NET, you must install the core library of Alibaba Cloud SDK for .NET (aliyun-net-sdk-core) and STS SDK for .NET, (aliyun-net-sdk-sts).
  • Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For more information about STS API operations, see What is STS?.
  • For more information about STS endpoints, see Endpoints.

Install the SDK for .NET

You can download the installation packages of the SDK for .NET from the following links:

Example

The following code provides an example on how to call the AssumeRole operation by using STS SDK for .NET. For more information about the request parameters that you must specify when you call the AssumeRole operation, see AssumeRole.

To obtain the sample code for other operations, visit OpenAPI Explorer.

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Sts.Model.V20150401;

namespace StsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Construct an Alibaba Cloud client that is used to initiate requests. 
            // Specify the AccessKey ID and AccessKey secret of the requester. The requester can be a RAM user or a RAM role. 
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            // Construct a request and specify request parameters. 
            var request = new AssumeRoleRequest();
            request.RoleArn = "<RoleArn>";
            request.RoleSessionName = "<RoleSessionName>";

            // Initiate the request and obtain a response. 
            try {
                var response = client.GetAcsResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}