Blazor WASM .NET Core Hosted .NET5:缺少 ClientId:无法按照 Google 身份验证示例进行 Google 身份验证

时间:2021-03-23 00:57:41

标签: authentication identityserver4 .net-5 blazor-webassembly

我使用 Visual Studio 2019 版本 16.9.2 创建了一个新解决方案。使用.NET5。

我使用 DotNetCLI 和命令创建了它

dotnet new blazorwasm -au Individual -ho -o HostingDashboard

我已将 Microsoft.AspNetCore.Authentication.Google Nuget 包添加到 Server 项目中。 在我的服务器项目 Startup.cs 文件中,我有以下 ConfigureServices 代码:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
                ;

            services.AddAuthentication()
                .AddIdentityServerJwt()
                ;

            services.AddAuthentication()
                .AddGoogle(options =>
                {
                    options.ClientId = Configuration["Google:ClientId"];
                    options.ClientSecret = Configuration["Google:ClientSecret"];
                    options.AuthorizationEndpoint += "?prompt=select_account";
                });

对于我的 program.cs 文件(服务器项目),我有:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
            .ConfigureAppConfiguration((hostContext, builder) => 
            {
                if (hostContext.HostingEnvironment.IsDevelopment())
                {
                    builder.AddUserSecrets<Program>();
                }
            });
    }

我已将以下内容添加到我的秘密中:

PM> dotnet user-secrets list
Google:ClientSecret = xxxxxx
Google:ClientId = xxxxxxxx.apps.googleusercontent.com

其中 xxxx 显然是混淆的东西,但从我们的 Google 控制台复制... 我还尝试使用名为“Authentication:Google:ClientId”等的机密,但这不会改变结果。

每次尝试在调试中运行时,都会出现以下错误页面:

System.ArgumentException: The 'ClientId' option must be provided. (Parameter 'ClientId')
   at Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions.Validate()
   at Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions.Validate(String scheme)
   at Microsoft.AspNetCore.Authentication.AuthenticationBuilder.<>c__DisplayClass4_0`2.<AddSchemeHelper>b__1(TOptions o)
   at Microsoft.Extensions.Options.ValidateOptions`1.Validate(String name, TOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass11_0.<Get>b__0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
   at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
   at IdentityServer4.Hosting.FederatedSignOut.FederatedSignoutAuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

任何有关如何解决此问题的建议将不胜感激。或者,这对于 Blazorwasm 和 .NET5 来说是不可能的?

0 个答案:

没有答案