在 ASP.NET Core 2.0 中设置社交身份验证

Setting Up Social Authentication in ASP.NET Core 2.0

我正在 ASP.NET Core 2.0 应用程序中设置社交登录,但未使用 Identity。

我只想通过 Facebook、Google 和 LinkedIn 对用户进行身份验证并接收他们的信息。我自己负责存储用户信息。

这是我到目前为止所做的,但出现了以下错误:

No authentication handler is configured to handle the scheme: facebook

Startup.cs 文件更改如下:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            // Added these lines for cookie and Facebook authentication
            services.AddAuthentication("MyCookieAuthenticationScheme")
                .AddCookie(options => {
                    options.AccessDeniedPath = "/Account/Forbidden/";
                    options.LoginPath = "/Account/Login/";
                })
                .AddFacebook(facebookOptions =>
                {
                    facebookOptions.AppId = "1234567890";
                    facebookOptions.AppSecret = "1234567890";
                });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            // Added this line
            app.UseAuthentication();

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

然后我有这个操作方法,我发送用户来确定我们用于身份验证的提供商,例如Facebook、Google 等。此代码来自我的 ASP.NET Core 1.1 应用程序,该应用程序运行良好。

    [AllowAnonymous]
    public async Task ExternalLogin(string provider, string returnUrl)
    {
        var properties = new AuthenticationProperties
        {
            RedirectUri = "Login/Callback"
        };

        // Add returnUrl to properties -- if applicable
        if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
            properties.Items.Add("returnUrl", returnUrl);

        // The ASP.NET Core 1.1 version of this line was
        // await HttpContext.Authentication.ChallengeAsync(provider, properties);
        await HttpContext.ChallengeAsync(provider, properties);

        return;
    }

我在点击 ChallangeAsync 行时收到错误消息。

我做错了什么?

No authentication handler is configured to handle the scheme: facebook

方案名称区分大小写。使用 provider=Facebook 而不是 provider=facebook,它应该可以工作。