Search code examples
visual-studioazure-active-directoryazure-identity

Accessing User Credential Value from Azure.Identity


I have connected my Visual studio to Azure Active Directory by setting and manually entering pass/email.

This credential shows when I go to Azure Serice Authenticator in Visual Studio under tools.

I want to access the ID associated. I came across this doc , and I want the AZURE_USERNAME , which must be associated with the id logged in.

I am unable to get a way by which I can access this value.


Does it mean that I can only access it if I explicitly set such value ?

-- Alternatively , is there any way by which I can access the Email ID of user signed in to Visual studio / or signed into Windows and not just User name


Solution

  • Yes of course we should preset environment variables.

    Then we can use them for EnvironmentCredential authentication. The reverse will not work.

    You can call Microsoft Graph API to get the user information.

    Sample reference here.

    You can implement the graphServiceClient like this:

    var credential = new DefaultAzureCredential();
    var token = credential.GetToken(
        new Azure.Core.TokenRequestContext(
            new[] { "https://graph.microsoft.com/.default" }));
    
    var accessToken = token.Token;
    var graphServiceClient = new GraphServiceClient(
        new DelegateAuthenticationProvider((requestMessage) =>
        {
            requestMessage
            .Headers
            .Authorization = new AuthenticationHeaderValue("bearer", accessToken);
    
            return Task.CompletedTask;
        }));
    

    A blog for your reference.