ASP.NET Core 2.0使用Services.Configure配置身份验证

时间:2018-03-07 15:05:49

标签: c# asp.net asp.net-core configuration asp.net-core-2.0

microsoft docs example显示了如何使用PostConfigurationOptions进行配置,但在此方案中,如果要更改配置,GoogleHandler IOptionsMonitor依赖项将不会收到更新。

AFAIK你应该能够只配置选项但我必须做错事,因为我得到以下异常:

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

我在新的ASP.NET Core 2.0 Web应用程序中重新创建了这个问题:

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"
        }
    }
}

如果我使用services.BuildServiceProvider().GetService<Microsoft.Extensions.Options.IOptionsMonitor<GoogleOptions>>().CurrentValue.ClientId,则clientId配置正确。

1 个答案:

答案 0 :(得分:1)

查看AuthenticationBuilder.cs实施解决了它。

AuthenticationSchemeOptions是一个命名依赖项,因此它应该像这样配置:

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