I created a sample Blazor project without Identity and tried to add to it Google sign in, but I keep getting this exception:
AuthenticationFailureException: The oauth state was missing or invalid.
I mainly followed the documentation but it seems to be missing some important parts.
This is all my code:
Program.cs
using GoogleOAuth.Components;using Microsoft.AspNetCore.Authentication.Cookies;using Microsoft.AspNetCore.Authentication.Google;var builder = WebApplication.CreateBuilder(args);builder.Services.AddAuthentication(options =>{ options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;}).AddCookie().AddGoogle(options =>{ options.ClientId = "..."; options.ClientSecret = "...";});builder.Services.AddAuthorization();// Add services to the container.builder.Services.AddRazorComponents();var app = builder.Build();// Configure the HTTP request pipeline.if (!app.Environment.IsDevelopment()){ 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.UseAuthentication();app.UseAuthorization();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>();app.Run();Home.razor
@page "/"<script src="https://accounts.google.com/gsi/client" async></script><div id="g_id_onload" data-client_id="..." data-login_uri="https://localhost:7091/signin-google" data-auto_prompt="false" class="rounded-xl"></div><div class="flex flex-col w-full items-center"><div class="g_id_signin" data-type="standard" data-size="large" data-theme="outline" data-text="sign_in_with" data-shape="rectangular" data-logo_alignment="left"></div></div>