liveBook · Manning

Authorization code flow

As explained in Building Distributed Applications that Work

The authorization code flow is an OAuth-based protocol in which a client exchanges an authorization code for tokens through a server-side interaction.

An authorization code flow lets a client obtain tokens through a server-side exchange: the identity provider returns a code, which the application exchanges for tokens. In an ASP.NET Core UI app, OpenID Connect options request a code response; tokens can be saved for later use. section 8.3

Configuration can include client credentials and a callback path for the identity provider’s response. PKCE adds protection and is especially relevant to public clients; it is optional in the configuration described here. section 8.3

Listing 8.1 Configuring authentication in the UI app startup
public static void ConfigureWebAppOpenIdConnect(this AuthenticationBuilder authentication)
{
    // Named options
    authentication.Services
        .AddOptions<OpenIdConnectOptions>(
            OpenIdConnectDefaults.AuthenticationScheme)
        .Configure<
            IConfiguration,
            IHttpClientFactory,
            IHostEnvironment>(Configure);

    // Unnamed options
    authentication.Services.AddOptions<OpenIdConnectOptions>()
        .Configure<
            IConfiguration,
            IHttpClientFactory,
            IHostEnvironment>(Configure);

    static void Configure(
        OpenIdConnectOptions options,
        IConfiguration configuration,
        IHttpClientFactory httpClientFactory,
        IHostEnvironment hostEnvironment)
    {
        var backchannelHttpClient =
            httpClientFactory.CreateClient(
                "OidcBackchannel");

        options.Backchannel = backchannelHttpClient;
        options.Authority =
            backchannelHttpClient
                .GetIdpAuthorityUri().ToString();
        options.ClientId = "webapp";
        options.ClientSecret =
            Environment
                .GetEnvironmentVariable(
                    "Identity__ClientSecret");
        options.ResponseType =
            OpenIdConnectResponseType.Code;
        options.SaveTokens = true;
        options.RequireHttpsMetadata =
            !hostEnvironment.IsDevelopment();
        options.MapInboundClaims = false;
    }
}

Inspect the UI app’s OpenID Connect configuration, including how its options are registered and configured. section 8.3

How Building Distributed Applications that Work covers it

How deep it goes
Defines the code response and server-side token exchange, then describes configuration options without detailing exchange execution.
Examples
Configures an ASP.NET Core UI app with a code response, saved tokens, and optional PKCE.
What you can do after
Configure ASP.NET Core OpenID Connect to request code responses, save tokens, and handle provider callbacks.
How it connects
Connects OAuth, OpenID Connect, identity providers, and PKCE through application authentication configuration.

Also explained in

Compare how 8 books explain authorization code flow →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.