ASP.NET Core MVC迁移到2.0:远程身份验证处理程序的SignInScheme无法设置为自身

时间:2018-01-12 16:32:33

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

我正在将ASP.NET Core MVC应用程序从1.0.2版迁移到2.0版。 我在这个应用程序中通过Facebook进行外部身份验证。 当我启动Web应用程序时,我遇到了这个错误:

  

InvalidOperationException:无法将远程身份验证处理程序的SignInScheme设置为自身。如果未明确设置,则使用AuthenticationOptions.DefaultSignInScheme或DefaultScheme。

我搜索了here,但现在对我没有帮助。 在我的Startup.cs中,“ConfigureServices”方法如下所示:

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

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // If you want to tweak Identity cookies, they're no longer part of IdentityOptions.
        services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/LogIn");
        services.AddAuthentication()
            .AddFacebook(options =>
            {
                options.AppId = Configuration["AuthenticationFacebookSettings:AppId"];
                options.AppSecret = Configuration["AuthenticationFacebookSettings:AppSecret"];
                options.SignInScheme = "Facebook";
                options.SaveTokens = true;
            });

        services.Configure<AuthenticationFacebookSettings>(Configuration.GetSection("AuthenticationFacebookSettings"));
        services.AddMvc();

        // Add application services.
                    .
                    .
            // my services
                    .
                    .
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
    }

“配置”方法如下所示:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseAuthentication();
        app.UseStaticFiles();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

我在这里遗漏了一些东西,因为一切看起来都不错...... 我花了很多时间在这个问题上,我一无所知...... 谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

@ps2goat的链接让我正确。 我改变了我的facebook选项(通过采用默认值):

services.AddAuthentication()
            .AddFacebook(options =>
            {
                options.AppId = Configuration["AuthenticationFacebookSettings:AppId"];
                options.AppSecret = Configuration["AuthenticationFacebookSettings:AppSecret"];
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.SaveTokens = true;
            });

事实上,在使用.NET core 2.0迁移时,我不得不更改&#34; SignInScheme&#34;。在我的情况下,我将其设置为默认值:

  

options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

更准确地说,启动应用程序还不够。 我不得不更新我的使用&#34; GetExternalAuthenticationSchemesAsync()&#34;方法而不是&#34; GetExternalAuthenticationSchemes()&#34;签约经理的方法。

在调整了一点我的代码之后,它终于有效了。谢谢@ps2goat