コード例 #1
0
        /// <summary>
        /// Initialize the IAM Client object and then create the IAM Access Key
        /// for the user.
        /// </summary>
        public static async Task Main()
        {
            var client   = new AmazonIdentityManagementServiceClient();
            var userName = "******";

            var response = await client.CreateAccessKeyAsync(new CreateAccessKeyRequest
            {
                UserName = userName,
            });

            AccessKey accessKey = response.AccessKey;

            Console.WriteLine($"{accessKey.AccessKeyId} created for {accessKey.UserName}");
        }
コード例 #2
0
        // snippet-end:[IAM.dotnetv3.CreateUserAsync]

        // snippet-start:[IAM.dotnetv3.CreateAccessKey]

        /// <summary>
        /// Create a new AccessKey for the user.
        /// </summary>
        /// <param name="client">The initialized IAM client object.</param>
        /// <param name="userName">The name of the user for whom to create the key.</param>
        /// <returns>A new IAM access key for the user.</returns>
        public static async Task <AccessKey> CreateAccessKeyAsync(
            AmazonIdentityManagementServiceClient client,
            string userName)
        {
            var request = new CreateAccessKeyRequest
            {
                UserName = userName,
            };

            var response = await client.CreateAccessKeyAsync(request);

            if (response.AccessKey is not null)
            {
                Console.WriteLine($"Successfully created Access Key for {userName}.");
            }

            return(response.AccessKey);
        }
コード例 #3
0
        /// <summary>
        /// Creates a new access key for the user represented by the userName
        /// parameter.
        /// </summary>
        /// <param name="client">The client object which will call
        /// CreateAccessKeyAsync.</param>
        /// <param name="userName">The name of the user for whom an access key
        /// is created by the call to CreateAccessKeyAsync.</param>
        /// <returns>Returns the response from the call to
        /// CreateAccessKeyAsync.</returns>
        public static async Task <CreateAccessKeyResponse> CreateNewAccessKeyAsync(AmazonIdentityManagementServiceClient client, string userName)
        {
            try
            {
                // Create an access key for the IAM user that can be used by the SDK
                var response = await client.CreateAccessKeyAsync(new CreateAccessKeyRequest
                {
                    // Use the user we created in the CreateUser example
                    UserName = UserName,
                });

                return(response);
            }
            catch (LimitExceededException e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
コード例 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="userName"></param>
        public async Task <AmazonAccountInfo> CreateIAMUser(string path, string userName)
        {
            var client  = new AmazonIdentityManagementServiceClient(AWSAccessKey, AWSSecurityKey);
            var request = new CreateUserRequest
            {
                UserName = userName,
                Path     = path
            };

            try
            {
                var response = await client.CreateUserAsync(request);

                // Assign the read-only policy to the new user
                await client.PutUserPolicyAsync(new PutUserPolicyRequest
                {
                    UserName       = userName,
                    PolicyDocument = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}",
                    PolicyName     = "AllAccessPolicy"
                });

                var accessKey = await client.CreateAccessKeyAsync(new CreateAccessKeyRequest
                {
                    // Use the user created in the CreateUser example
                    UserName = userName
                });

                var result = new AmazonAccountInfo()
                {
                    AccessKey   = accessKey.AccessKey.AccessKeyId,
                    SecurityKey = accessKey.AccessKey.SecretAccessKey
                };
                return(result);
            }
            catch (EntityAlreadyExistsException)
            {
                throw;
            }
        }