Skip to content

Instantly share code, notes, and snippets.

@buchizo buchizo/program.cs

Last active Apr 27, 2019
What would you like to do?
call Azure ARM REST API using user token via devicelogin
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp11
class Program
static void Main(string[] args)
Task.Run(() => GetToken()).Wait();
private const string ARMClientID = "1950a258-227b-4e31-a9cf-717495945fc2"; // <- this is Azure PowerShell client id
static async Task GetToken()
var authContextURL = "";
var authenticationContext = new AuthenticationContext(authContextURL);
var deviceresult = await authenticationContext.AcquireDeviceCodeAsync("", ARMClientID);
Console.WriteLine("device code: {0}", deviceresult.UserCode);
var token = await authenticationContext.AcquireTokenByDeviceCodeAsync(deviceresult);
// call to Azure ARM REST API using user token
var req = new HttpRequestMessage()
Method = HttpMethod.Get,
RequestUri = new Uri("")
req.Headers.Add("Authorization", token.CreateAuthorizationHeader());
var client = new HttpClient();
var res = await client.SendAsync(req);
var body = await res.Content.ReadAsStringAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.