This tutorial shows you how to enable your users to sign in with their Facebook account using a sample ASP.NET Core 2.0 project created on the previous page. Facebook authentication requires the Microsoft.AspNetCore.Authentication.Facebook NuGet package. We start by creating a Facebook App ID by following the official steps.
Create the app in Facebook
Navigate to the Facebook Developers app page and sign in. If you don’t already have a Facebook account, use the Sign up for Facebook link on the login page to create one.
Fill out the form and tap the Create App ID button.
On the Select a product page, click Set Up on the Facebook Login card.
The Quickstart wizard will launch with Choose a Platform as the first page. Bypass the wizard for now by clicking the Settings link in the menu on the left:
You are presented with the Client OAuth Settings page:
- Enter your development URI with /signin-facebook appended into the Valid OAuth Redirect URIs field (for example:
https://localhost:44320/signin-facebook). The Facebook authentication configured later in this tutorial will automatically handle requests at /signin-facebook route to implement the OAuth flow.
The URI /signin-facebook is set as the default callback of the Facebook authentication provider. You can change the default callback URI while configuring the Facebook authentication middleware via the inherited RemoteAuthenticationOptions.CallbackPath property of the FacebookOptions class.
Click Save Changes.
Click the Dashboard link in the left navigation.
When deploying the site you need to revisit the Facebook Login setup page and register a new public URI.
Link sensitive settings like Facebook
App ID and
App Secret to your application configuration using the Secret Manager. For the purposes of this tutorial, name the tokens
dotnet user-secrets set Authentication:Facebook:AppId <app-id> dotnet user-secrets set Authentication:Facebook:AppSecret <app-secret>
Configure Facebook Authentication
Add the Facebook service in the
ConfigureServices method in the Startup.cs file:
services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddAuthentication().AddFacebook(facebookOptions => facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"]; facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"]; );
The call to AddIdentity configures the default scheme settings. The AddAuthentication(String) overload sets the DefaultScheme property. The AddAuthentication(Action<AuthenticationOptions>) overload allows configuring authentication options, which can be used to set up default authentication schemes for different purposes. Subsequent calls to
AddAuthentication override previously configured AuthenticationOptions properties.
AuthenticationBuilder extension methods that register an authentication handler may only be called once per authentication scheme. Overloads exist that allow configuring the scheme properties, scheme name, and display name.
When the app requires multiple providers, chain the provider extension methods behind AddAuthentication:
services.AddAuthentication() .AddMicrosoftAccount(microsoftOptions => ... ) .AddGoogle(googleOptions => ... ) .AddTwitter(twitterOptions => ... ) .AddFacebook(facebookOptions => ... );
See the FacebookOptions API reference for more information on configuration options supported by Facebook authentication. Configuration options can be used to:
- Request different information about the user.
- Add query string arguments to customize the login experience.
Sign in with Facebook
Run your application and click Log in. You see an option to sign in with Facebook.
When you click on Facebook, you are redirected to Facebook for authentication:
Facebook authentication requests public profile and email address by default:
Once you enter your Facebook credentials you are redirected back to your site where you can set your email.
You are now logged in using your Facebook credentials:
- ASP.NET Core 2.x only: If Identity isn’t configured by calling
ConfigureServices, attempting to authenticate will result in ArgumentException: The ‘SignInScheme’ option must be provided. The project template used in this tutorial ensures that this is done.
- If the site database has not been created by applying the initial migration, you get A database operation failed while processing the request error. Tap Apply Migrations to create the database and refresh to continue past the error.
This article showed how you can authenticate with Facebook. You can follow a similar approach to authenticate with other providers listed on the previous page.
Once you publish your web site to Azure web app, you should reset the
AppSecretin the Facebook developer portal.
Authentication:Facebook:AppSecretas application settings in the Azure portal. The configuration system is set up to read keys from environment variables.