You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无Identity的Blazor项目配置Google登录时遭遇oauth state无效/缺失异常的问题求助

无Identity的Blazor项目配置Google登录时遭遇oauth state无效/缺失异常的问题求助

大家好,我最近在一个没有集成Identity的Blazor项目里尝试添加Google登录功能,但一直遇到这个异常:
AuthenticationFailureException: The oauth state was missing or invalid.

我基本是照着官方文档来配置的,但总感觉文档里漏掉了一些关键部分。下面是我所有的相关代码,麻烦各位帮忙排查一下问题所在:

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>

有没有朋友遇到过类似的问题?或者能看出我哪里配置错了吗?感谢大家的帮忙!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 11:24:37