azure devops sdk - how to use managed identity to connect to your azure devops

In the Azure Devops example, we seen quite alot of examples using PAT token to authenticate and access Azure Devops. Here we will try to change that and use managed identity instead. 

The following code show how you can achieve this. 

public async Task SetupConnectionUsingAzureIdentity(AzRepositoryConfiguration configuration)
{
    logger.LogInformation("Starting SetupConnectionUsingAzureIdentity, getting access token");

    var azureCredential = new DefaultAzureCredential();
    var token = await azureCredential.GetTokenAsync(new
TokenRequestContext(new string[] {
ScaffolderAppConstants.AzureDevopsApplicationIdScope }));

    VssCredentials creds = new
VssAadCredential(new VssAadToken(ScaffolderAppConstants.BearAuthorizationText,
token.Token));

    logger.LogInformation($"Authenticating and setting up connection to Azure Devops: {configuration.RepositoryEndpoint}");
    _vssConnection = new
VssConnection(new Uri(configuration.RepositoryEndpoint), creds);
}


Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20