In the previous article, we discussed the implementation of an identity-based authentication model in ASP.NET Core application. But sometimes, clients or users do not want to create new login credentials for their applications. They simply want to use some existing user credentials related to other accounts like Gmail, Facebook, Microsoft, Twitter, etc. So, at that time, we need to authenticate those users with their existing user credentials and allow access to our own application. In this article, we will discuss how to implement an external authentication provider in an ASP.NET Core application. We will discuss the step by step implementation process of the below external providers using ASP.NET Core Application.
- Gmail
- Facebook
- Twitter
- Microsoft
In this article, we will discuss how to implement external provider authentication using Gmail and Facebook.
Prerequisites
Before starting the implementation of external authentication providers, we first need to create an ASP.NET Core application with the Individual User Account Authentications. I have already discussed how to create a web application in ASP.NET Core in a step by step manner in the previous article. So, before starting this article, first read that article at the below link,
If you want, you can download the source code provided in the above article and then try external authentication provider-based implementation as mentioned in this article.
Implement GMAIL Login Authentication
Step 1
If we want to implement Gmail Login authentication in our web application, then we first need to create a client-id into the Google Sign-In API Website. For that, let us go to the following Google website.
https://developers.google.com/identity/sign-in/web/sign-in#before_you_begin
Step 2
Now, click on the "Configure Project" button.
Step 3
Now, select Create --> New Project options.

Step 4
Provide a meaningful Project Name and click on the "Next" button.

Step 5
Provide the product name in the "Configure OAuth Client" window.

Step 6
Now, select Web Server from the List.
Step 7
Now, in the Authorized Redirect URL, provide the application URL. The redirect URL will be like this - https://localhost:2213/signin-google.
You just need to change the hosted URL in the above example.
You just need to change the hosted URL in the above example.

Step 8
Click on the "Create" button.
Step 9
Now, the configuration is complete and we need to copy the Client Id and Client Secret from the popup window.

Step 10
Click the "Done" button.
Step 11
Now, open the appsettings.json file and create the two keys for client id and client secret id,
- {
- "ConnectionStrings": {
- "DefaultConnection": "Server=Deb;Database=DemoAuthentication;Trusted_Connection=True;MultipleActiveResultSets=true;user id=sa;password=xxxxxxxxxxxxxx;"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Warning"
- }
- },
- "AllowedHosts": "*",
- "Authentication:Google:ClientId": "xxxxxxxxxxxxxx",
- "Authentication:Google:ClientSecret": "xxxxxxxxxxxxxx",
- }
Step 12
Now open the following URL https://console.developers.google.com/projectselector/apis/library and login with your Google account credentials.

Step 13
Now select the Project name mentioned in the above and then search Google+ API options.
Step 14
Now click on Google API options
Step 15
Now click on Enabled Button.

Step 16
Now return back to our visual studio and open the startup.cs file.
Step 17
Now the add the below code in the ConfigureService() method.
- public void ConfigureServices(IServiceCollection services)
- {
- services.Configure<CookiePolicyOptions>(options =>
- {
- // This lambda determines whether user consent for non-essential cookies is needed for a given request.
- options.CheckConsentNeeded = context => true;
- options.MinimumSameSitePolicy = SameSiteMode.None;
- });
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(
- Configuration.GetConnectionString("DefaultConnection")));
- services.AddDefaultIdentity<IdentityUser>()
- .AddDefaultUI(UIFramework.Bootstrap4)
- .AddEntityFrameworkStores<ApplicationDbContext>();
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
- services.AddAuthentication().AddGoogle(googleOptions =>
- {
- googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
- googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
- });
- }









Michael AiyetanPosted Jan 25, 2021, 2:36 PM
Great work Debasis
Ravi KhambhatiPosted Jun 25, 2019, 11:10 AM
Great article. Can you please help me on how can I request additional details on "/Identity/Account/ExternalLogin?returnUrl=%2F&handler=Callback#_=_" page on top of the email address.
naveen semwalPosted May 2, 2019, 1:25 AM
I have followed all steps for google authentication but getting error while clicking on Google login button. Error: redirect_uri_mismatchThe redirect URI in the request, https://localhost:44331/signin-google, does not match the ones authorized for the OAuth client.