【问题标题】:No default authentication scheme has been set未设置默认身份验证方案
【发布时间】:2018-07-24 19:32:17
【问题描述】:

我正在从 IdentityServer4 1.x 升级到 IdentityServer4 2.0,这也意味着我已经升级到 .Net core 2.0 我知道这次升级有很多重大变化我已经做过一次之前,但由于某种原因,我陷入了这个错误。

警告:IdentityServer4.Startup[0] 未设置默认身份验证方案。需要设置默认方案。
警告:IdentityServer4.Startup[0] 未设置默认身份验证方案。需要设置默认方案。

在我调用app.UseIdentityServer(); 后,错误出现在Configure

配置方法:

 public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            if (Debugger.IsAttached)
                loggerFactory.AddConsole(Configuration);
            else
                loggerFactory.AddConsoleJson(Configuration);

            InitializeDatabase(app);

            // Stops microsoft from overwriting claim types to their proprietary ones
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            app.UseAuthentication();
            app.UseCors(builder =>
                builder.AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowAnyOrigin()
                    .AllowCredentials()
            );

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedProto
            });

            app.UseDeveloperExceptionPage();
            app.UseIdentityServer();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }

ConfigureServices 方法

public void ConfigureServices(IServiceCollection services)
    {
        var settingsSetup = Configuration.GetSection("Settings").Get<Settings>();
        settingsSetup.XenaConnectionUrl = Configuration["XenaPath"];
        services.AddSingleton(settingsSetup);

        var idsConnectionString = Configuration.GetConnectionString("XenaIdentityConnection");
        var xenaConnectionString = Configuration.GetConnectionString("XenaConnection");
        var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;


        services.AddDbContext<UserDbContext>(builder =>
            builder.UseSqlServer(xenaConnectionString));

        services.AddCors();
        services.AddMvc();

        services.TryAddScoped<UserManager, UserManager>();
        services.TryAddScoped<SignInManager, SignInManager>();
        services.TryAddSingleton(new XenaClient(Configuration));
        services.AddTransient<Services.IClaimsService, XenaClaimsService>();
        services.AddTransient<IProfileService, ProfileService>();

        // Sms service setup
        services.Configure<SmsOptions>(Configuration.GetSection("SmsOptions"));
        services.AddTransient<ISMSService, SMSService>();
        services.AddTransient<IResourceOwnerPasswordValidator, PasswordValidator>();

        services.AddIdentityServer()
            .AddSigningCredential(LoadCertificate())
            // this adds the config data from DB (clients, resources)
            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = builder =>
                    builder.UseSqlServer(idsConnectionString,
                        sql => sql.MigrationsAssembly(migrationsAssembly));
            })
            // this adds the operational data from DB (codes, tokens, consents)
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = builder =>
                    builder.UseSqlServer(idsConnectionString,
                        sql => sql.MigrationsAssembly(migrationsAssembly));

            })
            .AddProfileService<ProfileService>();

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
            options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
        });

        services.AddAuthorization(options =>
        {
            options.AddPolicy("Supporter", policy => policy.RequireClaim("supporter"));
        });
    }

我正在设置 DefaultAuthenticateScheme,所以我无法真正弄清楚问题所在。我已经在源代码中进行了挖掘,它似乎与验证有关我显然没有添加任何东西,但我无法弄清楚它是什么source

【问题讨论】:

标签: c# oauth .net-core identityserver4


【解决方案1】:

我发现了问题。以下所做的就是初始化它。它实际上并没有添加身份验证类型。

services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
        options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
    });

我用作参考的项目有以下内容

services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
                options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
            })
            .AddGoogle("Google", options =>
            {
                options.AccessType = "offline";
                options.SignInScheme = IdentityConstants.ExternalScheme;
                options.ClientId = Configuration.GetSection("Settings:GoogleClientId").Value;
                options.ClientSecret = Configuration.GetSection("Settings:GoogleClientSecret").Value;
            });

但我目前的项目目前不需要谷歌登录,所以我只是删除了那部分。这导致它失败,因为没有添加任何身份验证类型。

我刚刚删除了AddAuthentication 部分,现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2021-11-30
    • 2022-01-09
    • 2019-02-20
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多