Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure AD Get access token from authentication result object

Tags:

c#

azure

adal

I am new to Azure AD and trying to consume an api secured by the AD. I have successfully created and secured the api, but having hard time consuming it in my windows forms app. I have tried the documentation at Link, but getting a compile time error in this line

AuthenticationResult ar =
ac.AcquireToken("https://cloudidentity.net/WindowsAzureADWebAPITest",
"a4836f83-0f69-48ed-aa2b-88d0aed69652",
new Uri("https://cloudidentity.net/myWebAPItestclient"));

There is no such method in the ADAL now. There is async version which i tried, but takes different parameters

AuthenticationResult ar =
ac.AcquireTokenAsync("https://cloudidentity.net/WindowsAzureADWebAPITest",
"a4836f83-0f69-48ed-aa2b-88d0aed69652",
new Uri("https://cloudidentity.net/myWebAPItestclient"), IPlatformParameters);

Along with other information, it also wants IPlatformParameters object, which i have no idea about. I tried to pass null and go ahead, but then there is error in this line

string authHeader = ar.CreateAuthorizationHeader();

error is that there is no such method in ADAL for ar object. So i jumped to this tutorial as he was also using the windows forms app. The code he writes is

Task<AuthenticationResult> ar = authContext.AcquireTokenAsync("https://carsforher.onmicrosoft.com/SecuredCars_20160722021100", "2640aca3-a35e-42f8-8f6d-2e5fe1a09df4", new Uri("http://localhost"), null);
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ar.AccessToken).....

But there is no property as AccessToken for the ar object. Then i tried to download sample applications from Azure Documentation, but they also have written the exact same code, which unfortunately doesn't works. The version of ADAL i am using is 3.12.0.827. Kindly help me in figuring out how can i get the access token and consume the api.

like image 494
It's a trap Avatar asked Nov 09 '22 11:11

It's a trap


1 Answers

Your using the AcquireTokenAsync incorrectly: AcquireTokenAsync returns a task, not an AuthenticationResult object, and therefore method 'CreateAuthorizationHeader' and property 'AccessToken' are (not really) "missing".

A fixed version of your code will be:

AuthenticationResult ar = ac.AcquireTokenAsync("https://cloudidentity.net/WindowsAzureADWebAPITest",
    "a4836f83-0f69-48ed-aa2b-88d0aed69652",
    new Uri("https://cloudidentity.net/myWebAPItestclient"), IPlatformParameters).Result;

string authHeader = ar.CreateAuthorizationHeader();
string accessToken = ar.AccessToken;

or alternatively, so your code will really run async, you can add 'async' to the method signature and do:

AuthenticationResult ar = await ac.AcquireTokenAsync("https://cloudidentity.net/WindowsAzureADWebAPITest",
    "a4836f83-0f69-48ed-aa2b-88d0aed69652",
    new Uri("https://cloudidentity.net/myWebAPItestclient"), IPlatformParameters);

string authHeader = ar.CreateAuthorizationHeader();
string accessToken = ar.AccessToken;
like image 120
yonisha Avatar answered Nov 15 '22 11:11

yonisha