本文简要介绍了.NET SDK的安装方法,并提供了示例代码。
背景信息
- .NET SDK包含阿里云.NET SDK核心库(
aliyun-net-sdk-core
)和STS SDK(aliyun-net-sdk-sts
),两者都需要安装。 - OpenAPI Explorer提供在线调试API和动态生成SDK示例代码的功能,能显著降低API的使用难度,推荐您使用。
- 关于STS API的详情,请参见什么是STS。
- 关于STS的接入地址,请参见接入地址。
.NET SDK的安装方法
.NET SDK的安装方法,请参见快速开始。
.NET SDK安装包下载地址如下:
.NET SDK示例
下面为您提供AssumeRole API的.NET SDK示例代码。关于其他API,请访问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)
{
//构建一个阿里云客户端,用于发起请求。
//构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
DefaultAcsClient client = new DefaultAcsClient(profile);
//构建请求,设置参数。关于参数含义和设置方法,请参见API参考。
var request = new AssumeRoleRequest();
request.RoleArn = "<RoleArn>";
request.RoleSessionName = "<RoleSessionName>";
//发起请求,并得到响应。
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);
}
}
}
}