该问题是因为Google认证后没有设置重定向到Blazor应用程序的默认页(/Index)引起的。为解决此问题,可以在Google控制台中配置回调链接。以下是解决方案:
在Google控制台中创建OAuth 2.0客户端。
使用应用程序的URL和重定向URI进行配置。
将以下代码添加到Blazor应用程序的Startup.cs文件中的ConfigureServices方法中:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "Google";
})
.AddCookie("Cookies")
.AddGoogle(options =>
{
options.ClientId = Configuration["Google:ClientId"];
options.ClientSecret = Configuration["Google:ClientSecret"];
options.Events.OnCreatingTicket = ctx =>
{
var claims = new List
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
在应用程序中添加一个具有默认页的主页: @page "/" @page "/index" @inject HttpClient httpClient
在Google API中配置重定向URI(例如:https://localhost:5001/signin-google)
以这种方式配置后,您应该能够使用Google OAuth进行用户身份验证并使电子邮件确认页正常工作。