I want to use Individual Logins and Microsoft Auth on my Blazor WebApp. Now, for debugging purposes, I don't want to sign in every time to the app. So I thought I can solve it with cookies. The cookie works fine if I log in with an individual account. But when I'm using my Microsoft account to log in, there is no cookie stored and I have to log in again, when opening the browser again.
I followed the Docs on the Microsoft page, but I couldn't really find anything for this specific use-case (probably I'm just stupid).
The goal is, when opening the WebApp, I want to log in with my Microsoft account. When I close the browser and open it again, I want to be logged in. As mentioned before, this works for individual accounts currently but not for Microsoft accounts.
Here is the code:
using BlazorApp1.Components;using BlazorApp1.Components.Account;using BlazorApp1.Data;using Microsoft.AspNetCore.Components.Authorization;using Microsoft.AspNetCore.Identity;using Microsoft.EntityFrameworkCore;var builder = WebApplication.CreateBuilder(args);// 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.AddAuthentication(options => { options.DefaultScheme = IdentityConstants.ApplicationScheme; options.DefaultSignInScheme = IdentityConstants.ExternalScheme; }).AddMicrosoftAccount(microsoftOptions => { microsoftOptions.ClientId = builder.Configuration["Authentication:Microsoft:ClientId"]; microsoftOptions.ClientSecret = builder.Configuration["Authentication:Microsoft:ClientSecret"]; }) .AddIdentityCookies();var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));builder.Services.AddDatabaseDeveloperPageExceptionFilter();builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>() .AddSignInManager() .AddDefaultTokenProviders();builder.Services.AddSingleton<IEmailSender<ApplicationUser>, 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.UseHttpsRedirection();app.UseStaticFiles();app.UseAntiforgery();app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();// Add additional endpoints required by the Identity /Account Razor components.app.MapAdditionalIdentityEndpoints();app.Run();It's basically the default template from the Blazor Example with ASP.NET Identity (if you want to try it by yourself).
Thanks in advance for visisting/helping!