简体   繁体   中英

ASP.NET Core 2.0 Configuring Authentication with Services.Configure

The microsoft docs example shows how to configure using PostConfigurationOptions but in this scenario if the configuration were to change the GoogleHandler IOptionsMonitor dependency will not receive the update.

AFAIK you should be able to just configure the options but I must be doing something wrong as I get the following exception:

ArgumentException: The 'ClientId' option must be provided.
Parameter name: ClientId
    Microsoft.AspNetCore.Authentication.OAuth.OAuthOptions.Validate()

I have recreated the problem in a fresh ASP.NET Core 2.0 Web Application:

Startup.cs

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace TestWebApplication {
    public class Startup {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration) {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services) {
            services
                .AddAuthentication(o => {
                    o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    o.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
                })
                .AddCookie()
                .AddGoogle()
                .Services.Configure<GoogleOptions>(Configuration.GetSection("Authentication:Google"));

            services.AddMvc(o => {
                o.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
            app.UseDeveloperExceptionPage()
               .UseStaticFiles()
               .UseMvc(routes => {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
        }
    }
}

Program.cs

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace TestWebApplication {
    public class Program {
        public static void Main(string[] args) {
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build()
                .Run();
        }
    }
}

appsettings.json

{
    "Authentication": {
        "Google": {
            "ClientId": "xxxxxx",
            "ClientSecret": "xxxxxxx"
        }
    }
}

If I use services.BuildServiceProvider().GetService<Microsoft.Extensions.Options.IOptionsMonitor<GoogleOptions>>().CurrentValue.ClientId the clientId is configured correctly.

Looking at the AuthenticationBuilder.cs implementation solved it.

AuthenticationSchemeOptions is a named dependency so it should be configured like so:

services
    .AddAuthentication(o => {
         o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
         o.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
     })
    .AddCookie()
    .AddGoogle()
    .Services.Configure<GoogleOptions>(GoogleDefaults.AuthenticationScheme, Configuration.GetSection("Authentication:Google"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM