Search code examples
azureasp.net-coreazure-active-directoryazure-ad-b2cazure-ad-b2b

Grab Access token - ASP Net Cor 2.x webapp ( work or School Account )


I have created a simple Web application using Visual Studio 2017. By selecting below template and after providing my domain details, VS created a project which is ready and protected by Azure AD.

enter image description here

Now, after logging in, I want to capture the Access token returned by the Azure AD Identity Server. I want to examine that token in https://jwt.io/

Is there any way to plug in code in this framework to gram the access token?


Solution

  • Now, after logging in, I want to capture the Access token returned by the Azure AD Identity Server. I want to examine that token in https://jwt.io/

    That seems you just want to check the ID token in jwt.io .ID token is sent to the client application as part of an OpenID Connect flow and is used by the client to authenticate the user. Please refer to document : ID tokens .

    Access tokens enable clients to securely call APIs protected by Azure . Please refer to document : Azure Active Directory access tokens .

    For testing , one way to get the token is from one of the OpenIdConnectEvents :

    services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
    {
        options.Events = new OpenIdConnectEvents
        {
    
            OnTokenValidated = async ctx =>
            {
                var Token = ctx.SecurityToken.RawData.ToString();
    
            },
    
        };
    });
    

    If you want to get the access token for accessing resource which protected by Azure AD , you should use ADAL(Azure AD V1.0 endpoint) to obtain the token , see code sample here . Or use MSAL if you are using Azure AD V2.0 endpoint .