question

MortenGabrielsen-6743 avatar image
0 Votes"
MortenGabrielsen-6743 asked ·

Getting hold of the access token in the sign in process to Azure Active Directory

Hi,

I want to sign in users to their Microsoft account (Work or school account or Outlook.com accounts). I've managed to do this through example code. However, for my project I want to store the access token for later access. The problem is that I don't know where I can find the access token in the code. I have basic knowledge of OAUTH and using GET and POST commands, but no expert in ASP.NET, Razor or the libraries that handles the authentication or sign in process.

I've created a project as follows, which contained example code to sign in users:

Create new project - ASP.NET Core Web Application - Web Application (Model-View-Controller).
Choose .NET Core 2.2.
Authentication - click "change".
Choose "Work or school accounts". Information: For applications that authenticate users with Active Directory, Microsoft Azure Active Directory, or Office 365.
Enter domain name of my Microsoft Azure Active Directory tenant.
Create.

Running this application, it will ask me to log in and to consent that the application can get access to my account. It works as intended.

But is there any place in the code where I can debug and have a look at the access token? I can't even locate the POST command in the project code, which I assume is called behind the scenes to get hold of it.

Thanks.

azure-active-directory
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KalyanChanumolu-MSFT avatar image
0 Votes"
KalyanChanumolu-MSFT answered ·

You can use the OnTokenValidated event to capture the token for debugging.
Here is a sample code from Startup.cs

 services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
          .AddAzureAD(options => Configuration.Bind("AzureAd", options));
    
 services.Configure(AzureADDefaults.OpenIdScheme, options =>
 {
     options.Authority = options.Authority + "/v2.0/";         
    
     options.TokenValidationParameters.ValidateIssuer = false; 
    
     options.Events = new OpenIdConnectEvents
     {
         OnTokenValidated = context =>
         {
             // Access Token
             var accessToken = context.SecurityToken.RawData;
    
             return Task.CompletedTask;
         },
    
         OnAuthenticationFailed = context =>
         {
             Console.WriteLine($"Token Authentication failed with error: " + context.Exception.Message);
             return Task.CompletedTask;
         }
     };
 });
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MortenGabrielsen-6743 avatar image
0 Votes"
MortenGabrielsen-6743 answered ·

Thanks, I get these errors when I try this (also tried it in .NET Core 3.0):

The type arguments for method 'OptionsServiceCollectionExtensions.Configure(IServiceCollection, string, Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

If I try to specify the type of the arguments as CookiePolicyOptions, which was default after the project had been created, then I get errors like

'BinderOptions' does not contain a definition for 'Events' and no accessible extension method 'Events' accepting a first argument of type 'BinderOptions' could be found (are you missing a using directive or an assembly reference?)

(not only for Events but also for Authority and TokenValidationParameters).

Am I missing something?

The original services.Configure block in my Startup.cs file has the type CookiePolicyOptions and no argument like AzureADDefaults.OpenIdScheme.

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I created a new MVC app from VS 2019 targeting .net core 3.0.
I dont find CookiePolicyOptions being set by default.

I have attached my startup.cs file for reference.


link text


1 Vote 1 · ·
startup.txt (3.9 KiB)

I got this working now both in .net core 2.2 and 3.0. I get hold of the token and can debug it.

0 Votes 0 · ·