InvalidOperationException:没有为方案“Identity.External”注册身份验证处理程序

问题描述 投票:0回答:1

我在 .NET Core Web API 项目中陷入了 Google 身份验证。 我使用 Google 作为我的外部身份验证器,一切正常,除了当调用返回到我的处理程序时,发生异常。详情请看这里:

我正在使用 .NET 8 最小 API、Identity 和 EF core。 这就是我添加身份验证和授权的方式:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(googleOptions =>
{
    googleOptions.ClientId = configuration["Auth:Google:ClientId"] ?? string.Empty;
    googleOptions.ClientSecret = configuration["Auth:Google:ClientSecret"] ?? string.Empty;

    googleOptions.Scope.Add("email"); // to get primary email address
    googleOptions.Scope.Add("profile"); // to get personal public info like name, image etc.
});

services.AddAuthorization();

这就是我添加身份的方式

services.AddIdentityCore<ApplicationUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<OctuFitContext>()
    .AddSignInManager<SignInManager<ApplicationUser>>();

这就是我挑战身份验证的方式

var redirectUrl = "api/auth/google/handler";
var properties = signInManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);

return await Task.FromResult(Results.Challenge(properties));

问题是当呼叫返回到我的处理程序时,我尝试像这样获取外部登录信息

var info = await signInManager.GetExternalLoginInfoAsync();

我得到以下异常

No authentication handler is registered for the scheme 'Identity.External'. The registered schemes are: Cookies, Google. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("Identity.External",...)?

同样的代码正在我的另一个项目中与控制器一起工作。我知道这与控制器无关,但我错过了什么吗?我在这里真的很困惑,花了很多时间来解决这个问题,但都是徒劳的:(

google-api-dotnet-client .net-8.0 minimal-apis asp.net-authentication
1个回答
0
投票

尝试将以下内容添加到您的身份验证处理程序中

options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

除了您设置的其他两个选项之外

© www.soinside.com 2019 - 2024. All rights reserved.