Obsidian Delta
Obsidian Delta

Reputation: 81

Guidance on integrating Azure B2C with a Blazor Web App

We are trying to integrate Azure B2C with a Blazor Web App. There is some general guidance on doing this here however we are still unable to find a solution. Blazor Server App through Visual Studio makes it simple to do this integration when you create the project, however with the new Blazor Web App we are only given the option of individual login. Any guidance would greatly be appreciated.

We tried to follow the guidelines outlined here but to no avail. Here is what we have so far in the program.cs file for Blazor Web App but when we run it we get the error message "An action cannot use both form and JSON body parameters.":

using AzureB2CTesting2.Components;
using AzureB2CTesting2.Components.Account;
using AzureB2CTesting2.Data;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;

var builder = WebApplication.CreateBuilder(args);


// Add configuration to the container
builder.Configuration.AddJsonFile("appsettings.json"); // Adjust the file path as needed


// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddCascadingAuthenticationState();
//builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();

builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration, "AzureB2C");

builder.Services.AddControllersWithViews()
    .AddMicrosoftIdentityUI();

builder.Services.AddRazorPages();

//Configuring appsettings section AzureAdB2C, into IOptions
builder.Services.AddOptions();
builder.Services.Configure<OpenIdConnectOptions>(builder.Configuration.GetSection("AzureB2C"));

IdentityNoOpEmailSender>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseAuthentication();
app.UseAuthorization();

app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();

app.Run();

Update We created a Blazor Server App that uses Azure B2C and then converted it to a Blazor Web App using guidance found here

Just make sure that the App.razor and Routes.razor pages are in the root folder.

Upvotes: 0

Views: 384

Answers (1)

Obsidian Delta
Obsidian Delta

Reputation: 81

We created a Blazor Server App that uses Azure B2C and then converted it to a Blazor Web App using guidance found here

Just make sure that the App.razor and Routes.razor pages are in the root folder.

Upvotes: 0

Related Questions