stephen
stephen

Reputation: 23

.NET Core 3.1 MVC Web application - accessing AzureADB2C tokens

Are there any code samples of how to access the AzureAdB2C token for later use in Azure function calls? The web project has already incorporated the Azure authentication using OpenIdConnect. The following is the setup in Startup.cs:

services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddAzureAdB2C(options => Configuration.Bind("Authentication:" + AzureCompany, options))
        .AddCookie();

Upvotes: 0

Views: 127

Answers (2)

Nan Yu
Nan Yu

Reputation: 27588

Not sure about your requirement , but with OIDC middleware , you can set SaveTokens to true :

services.Configure<OpenIdConnectOptions>(AzureADB2CDefaults.OpenIdScheme, options =>
{

    ...
    options.SaveTokens = true;
    ...
});

Put above config below B2C authentication , and after user is authenticated , you can get that in controller like :

string idToken = await HttpContext.GetTokenAsync("id_token");

Upvotes: 1

Manish
Manish

Reputation: 1197

I would not recommend this approach, correct me if I am wrong, you are trying to use same Bearer token for function app authentication?
instead of this you can generate a new token for function app, and use that.
you can generate a token using ms APIs.
For more info -> https://westerndevs.com/Functions-aad-authentication/
Request a token Guide-> https://learn.microsoft.com/en-us/azure/active-directory-b2c/access-tokens

Upvotes: 0

Related Questions