全部产品
访问控制

示例

更新时间:2017-06-07 13:26:11   分享:   

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Sts.Model.V20150401;

namespace sts_net_sdk_sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 构建一个 Aliyun Client, 用于发起请求
            // 构建Aliyun Client时需要设置AccessKeyId和AccessKeySevcret
            // STS是Global Service, API入口位于华东 1 (杭州) , 这里Region填写"cn-hangzhou"
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<access-key-id>", "<access-key-secret>");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            // 构造AssumeRole请求
            AssumeRoleRequest request = new AssumeRoleRequest();
            // 指定角色Arn
            request.RoleArn = "<role-arn>";
            request.RoleSessionName = "<role-session-name>";

            // 可以设置Token有效期,可选参数,默认3600秒;
            // request.DurationSeconds = 3600;
            // 可以设置Token的附加Policy,可以在获取Token时,通过额外设置一个Policy进一步减小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);
                //Token过期时间;服务器返回UTC时间,这里转换成北京时间显示;
                Console.WriteLine("Expiration: " + DateTime.Parse(response.Credentials.Expiration).ToLocalTime());
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            Console.ReadLine();
        }
    }
}
本文导读目录
本文导读目录
以上内容是否对您有帮助?