Get AWS caller Identity with .net core

0

When I execute this with the aws cli, i.ex. inside a fargate task, I can see the UserId that my application is going to use

    aws sts get-caller-identity

with this output on the console

    {
        "Arn": "arn:aws:sts::643518765421:assumed-role/url_process_role/6ae81f92-66f3-30de-1eaa-3a7d1902bad9",
        "UserId": "ARDYOAZLVOAQXTT5ZXTV4:4ea81f97-66f3-40de-beaa-3a7d1902bad9",
        "Account": "692438514791"
    }

I would like to get the same information but using the C# SDK. I tried with the methods exposed in https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html where I can see some account related details but not the assigned UserId.

So far I've tried with this but I cannot see any profile when running in a Fargate task.

var awsChain = new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain();
System.Console.WriteLine($"Found {awsChain.ListProfiles().Count} AWS profiles.");

My final goal is to get it and add to some task processed with Fargate to save a correlation Id in the database when something fails and easily find the Fargate log stream.

Edited by: guillemsola on May 17, 2019 3:50 PM

asked 5 years ago930 views
1 Answer
0

I finally found it. The example will only work inside an AWS task as the endpoint is not publicly available.

var getSessionTokenRequest = new GetSessionTokenRequest
                    {
                        DurationSeconds = 7200 // seconds
                    };
                    var stsClient = hostContext.Configuration.GetAWSOptions().CreateServiceClient<IAmazonSecurityTokenService>();
                    var iden = stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest { }).Result;
                    System.Console.WriteLine($"A={iden.Account} ARN={iden.Arn} U={iden.UserId}");
answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions