Connect to Azure DevOps using PAT in C#
Post by: syed hussain in All Azure DevOps C#
Summary
Here is a code snippet demonstrating how to connect to Azure DevOps using C#.
The code below assumes that you have generated a PAT. For instructions on how to do this, go to Authenticate access with a Personal Access Token.
Postman configuration
Once you’ve received you PAT, you can make HTTP Requests using the following configuration:
HTTP Method | GET |
URI | https://{org}.visualstudio.com/defaultcollection/_apis/projects?api-version=1.0 |
Under Authorization, select Basic Auth.
Now under Password add your PAT. Leave Username blank.
That’s all that’s required to configure Postman with PAT.
C# Code to access DevOps items
// nuget:Microsoft.TeamFoundationServer.Client
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EnterpriseArchitectureProject
{
class Program
{
static void Main(string[] args)
{
const string ORG = "https://dev.azure.com/eax360";
const string PAT = "2d2eocomyu4k5geho4cfxhp5a";
if (ORG != null)
{
Uri orgUrl = new Uri(ORG); // Organization URL, for example: https://dev.azure.com/eax360
String personalAccessToken = PAT; // See https://docs.microsoft.com/azure/devops/integrate/get-started/authentication/pats
int workItemId = int.Parse("2422"); // ID of a work item, for example: 12
// Create a connection
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
// Show details a work item
ShowWorkItemDetails(connection, workItemId).Wait();
}
}
static private async Task ShowWorkItemDetails(VssConnection connection, int workItemId)
{
// Get an instance of the work item tracking client
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
try
{
// Get the specified work item
WorkItem workitem = await witClient.GetWorkItemAsync(workItemId);
// Output the work item's field values
foreach (var field in workitem.Fields)
{
Console.WriteLine(" {0}: {1}", field.Key, field.Value);
}
}
catch (AggregateException aex)
{
VssServiceException vssex = aex.InnerException as VssServiceException;
if (vssex != null)
{
Console.WriteLine(vssex.Message);
}
}
}
}
}
Alternative approach that uses Client Libraries for an Interactive Login. The client libraries enable dual-support (Azure AD and Windows Authentication).
public static void GetProjects()
{
const string azureDevOpsOrganizationUrl = "https://dev.azure.com/eax360";
try
{
//Based on collection URL will either start an interactive login session or use local Windows credential authentication
VssConnection connection = new VssConnection(new Uri(azureDevOpsOrganizationUrl), new VssClientCredentials());
ProjectHttpClient projectClient = connection.GetClient<ProjectHttpClient>();
IEnumerable<TeamProjectReference> projects = projectClient.GetProjects().Result;
foreach (TeamProjectReference p in projects)
{
Console.WriteLine(p.Name);
}
}
catch (Exception ex)
{
Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message);
}
}
Tags: authentication azure devops c# DevOps