Search code examples
asp.net-corefacebook-authenticationasp.net-authentication

Setting Up Social Authentication in ASP.NET Core 2.0


I'm setting up social login in an ASP.NET Core 2.0 application without using Identity.

I simply want to authenticate the user through Facebook, Google and LinkedIn and receive their info. I handle storing user info myself.

Here's what I've done so far which is giving me the following error:

No authentication handler is configured to handle the scheme: facebook

Here's the Startup.cs file changes:

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?}");
            });
        }

I then have this action method where I send the user to determine the provider we're using for authenticaiton e.g. Facebook, Google, etc. This code came from my ASP.NET Core 1.1 app which is working fine.

    [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;
    }

I'm getting the error message when I hit the ChallangeAsync line.

What am I doing wrong?


Solution

  • No authentication handler is configured to handle the scheme: facebook

    Scheme names are case-sensitive. Use provider=Facebook instead of provider=facebook and it should work.