The following examples show how to call GetSessionToken
and
AssumeRole
operations and pass MFA authentication parameters. No
permissions are required to call GetSessionToken
, but you must have a policy
that allows you to call AssumeRole
. The credentials returned are then used to
list all S3 buckets in the account.
Calling GetSessionToken with
MFA authentication
The following example shows how to call GetSessionToken
and pass MFA
authentication information. The temporary security credentials returned by the
GetSessionToken
operation are then used to list all S3 buckets in the
account.
The policy attached to the user who runs this code (or to a group that the user is in)
provides the permissions for the returned temporary credentials. For this example code,
the policy must grant the user permission to request the Amazon S3 ListBuckets
operation.
The following code examples show how to use GetSessionToken
.
- AWS CLI
-
To get a set of short term credentials for an IAM identity
The following
get-session-token
command retrieves a set of short-term credentials for the IAM identity making the call. The resulting credentials can be used for requests where multi-factor authentication (MFA) is required by policy. The credentials expire 15 minutes after they are generated.aws sts get-session-token \ --duration-seconds
900
\ --serial-number"YourMFADeviceSerialNumber"
\ --token-code123456
Output:
{ "Credentials": { "AccessKeyId": "ASIAIOSFODNN7EXAMPLE", "SecretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY", "SessionToken": "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpRV3zrkuWJOgQs8IZZaIv2BXIa2R4OlgkBN9bkUDNCJiBeb/AXlzBBko7b15fjrBs2+cTQtpZ3CYWFXG8C5zqx37wnOE49mRl/+OtkIKGO7fAE", "Expiration": "2020-05-19T18:06:10+00:00" } }
For more information, see Requesting Temporary Security Credentials in the AWS IAM User Guide.
-
For API details, see GetSessionToken
in AWS CLI Command Reference.
-
Calling AssumeRole with MFA
authentication
The following examples show how to call AssumeRole
and pass MFA
authentication information. The temporary security credentials returned by
AssumeRole
are then used to list all Amazon S3 buckets in the
account.
For more information about this scenario, see Scenario: MFA protection for cross-account delegation.
The following code examples show how to use AssumeRole
.
- AWS SDK for .NET
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. using System; using System.Threading.Tasks; using Amazon; using Amazon.SecurityToken; using Amazon.SecurityToken.Model; namespace AssumeRoleExample { class AssumeRole { /// <summary> /// This example shows how to use the AWS Security Token /// Service (AWS STS) to assume an IAM role. /// /// NOTE: It is important that the role that will be assumed has a /// trust relationship with the account that will assume the role. /// /// Before you run the example, you need to create the role you want to /// assume and have it trust the IAM account that will assume that role. /// /// See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html /// for help in working with roles. /// </summary> private static readonly RegionEndpoint REGION = RegionEndpoint.USWest2; static async Task Main() { // Create the SecurityToken client and then display the identity of the // default user. var roleArnToAssume = "arn:aws:iam::123456789012:role/testAssumeRole"; var client = new Amazon.SecurityToken.AmazonSecurityTokenServiceClient(REGION); // Get and display the information about the identity of the default user. var callerIdRequest = new GetCallerIdentityRequest(); var caller = await client.GetCallerIdentityAsync(callerIdRequest); Console.WriteLine($"Original Caller: {caller.Arn}"); // Create the request to use with the AssumeRoleAsync call. var assumeRoleReq = new AssumeRoleRequest() { DurationSeconds = 1600, RoleSessionName = "Session1", RoleArn = roleArnToAssume }; var assumeRoleRes = await client.AssumeRoleAsync(assumeRoleReq); // Now create a new client based on the credentials of the caller assuming the role. var client2 = new AmazonSecurityTokenServiceClient(credentials: assumeRoleRes.Credentials); // Get and display information about the caller that has assumed the defined role. var caller2 = await client2.GetCallerIdentityAsync(callerIdRequest); Console.WriteLine($"AssumedRole Caller: {caller2.Arn}"); } } }
-
For API details, see AssumeRole in AWS SDK for .NET API Reference.
-