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