添加 Google 身份验证时,网站存在访问冲突

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

我正在尝试添加 google 身份验证并已安装 nuget oacjage Microsoft.AspNetCore.Authentication.Google v. 7.0.14

但是当我尝试将其添加到服务时,该网站停止并显示消息

程序“[20972] LT.LoveMatch.Natural.exe”已退出并带有代码 3221225477 (0xc0000005)“访问冲突”。

代码:

GoogleConfigSection config = new GoogleConfigSection();
builder.Configuration.GetSection("Google").Bind(config);

builder.Services.AddAuthentication().AddGoogle(opt =>
{
    opt.ClientId = config.ClientId;
    opt.ClientSecret = config.ClientSecret;
});

刚刚创建了一个新的网站项目,除了安装 nuget 包 Microsoft.AspNetCore.Authentication.Google 8.0.3 并添加服务之外什么也没做。在我添加服务之前它有效,然后由于代码访问冲突而失败。

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddAuthentication().AddGoogle(opt => {
    opt.ClientId = "clientId";
    opt.ClientSecret = "secret";
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // 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.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

有什么想法吗?谢谢!

c# asp.net-core authentication google-oauth
1个回答
0
投票

有点晚了,但可能对其他寻找答案的人有用。对我来说解决类似问题的是添加身份验证默认值。不知道为什么 Microsoft 文档中没有提到它,或者我是否错过了它。无论如何,将代码编辑为以下内容应该可以修复错误:

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = BearerTokenDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = BearerTokenDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddBearerToken()
.AddGoogle(options =>
{
    options.ClientId = googleClientId;
    options.ClientSecret = googleClientSecret;
});

如果您也选择 cookie 身份验证,这应该可行。

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