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

gefragt vor 5 Jahren786 Aufrufe
1 Antwort
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}");
beantwortet vor 5 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen