Пример #1
0
        /// <summary>
        /// This method creates a new IAM user.
        /// </summary>
        /// <param name="client">The IAM client object.</param>
        /// <param name="request">The user creation request.</param>
        /// <returns>The object returned by the call to CreateUserAsync.</returns>
        public static async Task <User> CreateNewUserAsync(AmazonIdentityManagementServiceClient client, CreateUserRequest request)
        {
            CreateUserResponse response = null;

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

                // Show the information about the user from the response.
                Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------");
                Console.WriteLine($"New user: {response.User.UserName} ARN = {response.User.Arn}.");
                Console.WriteLine($"{response.User.UserName} has {response.User.PermissionsBoundary}.");
            }
            catch (EntityAlreadyExistsException ex)
            {
                Console.WriteLine($"{ex.Message}");
            }

            if (response is not null)
            {
                return(response.User);
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        public static string CreateTestUser(AmazonIdentityManagementServiceClient client)
        {
            string username = "******" + DateTime.Now.Ticks;

            client.CreateUserAsync(new CreateUserRequest()
            {
                UserName = username, Path = TEST_PATH
            }).Wait();
            return(username);
        }
Пример #3
0
        internal static string CreateTestUser(AmazonIdentityManagementServiceClient client, string testPrefix)
        {
            var    prefix   = MakePath(testPrefix);
            string username = "******" + DateTime.Now.Ticks;

            client.CreateUserAsync(new CreateUserRequest()
            {
                UserName = username, Path = prefix
            }).Wait();
            return(username);
        }
Пример #4
0
        // snippet-start:[IAM.dotnetv3.CreateUserAsync]

        /// <summary>
        /// Create a new IAM user.
        /// </summary>
        /// <param name="client">The initialized IAM client object.</param>
        /// <param name="userName">A string representing the user name of the
        /// new user.</param>
        /// <returns>The newly created user.</returns>
        public static async Task <User> CreateUserAsync(
            AmazonIdentityManagementServiceClient client,
            string userName)
        {
            var request = new CreateUserRequest
            {
                UserName = userName,
            };

            var response = await client.CreateUserAsync(request);

            return(response.User);
        }
Пример #5
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;
            }
        }
        /// <summary>
        /// Initializes the IAM Client object and then uses it to create
        /// a new IAM User.
        /// </summary>
        public static async Task Main()
        {
            var    client = new AmazonIdentityManagementServiceClient();
            User   readOnlyUser;
            string userName    = "******";
            var    userRequest = new CreateUserRequest
            {
                UserName = userName,
            };

            var response = await client.CreateUserAsync(userRequest);

            readOnlyUser = response.User;

            if (readOnlyUser is not null)
            {
                Console.WriteLine($"Successfully created {readOnlyUser.UserName}");
            }
            else
            {
                Console.WriteLine("Could not create user.");
            }
        }