【问题标题】:ASP.NET Core 2.0 Configuring Authentication with Services.ConfigureASP.NET Core 2.0 使用 Services.Configure 配置身份验证
【发布时间】:2018-08-15 17:49:12
【问题描述】:

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&lt;Microsoft.Extensions.Options.IOptionsMonitor&lt;GoogleOptions&gt;&gt;().CurrentValue.ClientId,clientId 配置正确。

【问题讨论】:

    标签: c# asp.net asp.net-core configuration asp.net-core-2.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"));
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 2018-06-23
    • 1970-01-01
    • 2018-01-30
    • 2018-01-28
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多