Google Authentication in ASP.NET Core Application

Google Authentication in ASP.NET Core Application


In this blog, we will see how to authenticate the ASP.NET Core application with Google account. To achieve this, we have to setup the Google+ API in our google developer console then configure that client credentials in our ASP.NET Core application.

Setup Google Developer Console

To setup google developer console, follow the steps given in below blog,

Note: Use project name as “GoogleAuth”

Need to enable Google + API in Library tab.

Search for Google+ API in the search bar and select Google+ API from the search results. Refer to the below screenshot,

Once we selected Google+ API, you can find the button to enable this API. We must click this Enable button to enable this API. Refer to the below screenshot,

Once we enabled the Google+ API, we will be redirected to API home page. Here we must create credential which we are going to use with our ASP.NET Core web application.

We have follow four steps to obtain the credential.

First step, we need to fill out details about what kind of credentials we need.

  • Which API are you using?  – Google+ API
  • Where will you be calling the API from? – Web server.
  • What date will you be accessing? – User data

Then click on “What credentials do I need?” Button.

Then, we need to fill out details about OAuth Client ID.

  • Name: Web Client 1 (default it will be filled)
  • Authorized JavaScript origins: Leave as empty
  • Authorize Redirect URLs: We need to give our web applications root URL and need to append that with /signin-google. Here I have used my localhost application root URL http://localhost:61364/signin-google

Then, Set up the OAuth 2.0 consent screen.

  • Email address: Google email address will be selected here by default.
  • Product name: We need to enter the product name which we need to show to your users in our application.

Note: Don’t use the word ‘Google’ in this product name. If you use the work Google in product name, this will throw the error example “GoogleAuthApplication”.

  • Click continue once filled.

Download Credentials, here we can see our client id. We can download the credentials information using the download link, this will download the file in JSON format.

  • Downloaded file name: client_id.json
  • You can find the ClientId and ClientSecretfield in this file. We will use these two fields to configure our google authentication application.

Create ASP.NET Core Web Application

To create an ASP.NET Core application follow the steps given in below blog.

Configure ASP.NET Core Web Application to use Google Authentication

Step 1: We need to store the google ClientID and ClientSecret values in our application. We can use Manage User Secrets to store these values.

Step 2: Right click on project in Solution Explorer and select Manage User Secrets. This will open a secrets.json file.

Step 3: Place our Google ClientID and ClientSecret in below format and save it.

  "Authentication:Google:ClientId": "Your Google ClientId here",  
  "Authentication:Google:ClientSecret": "Your Google ClientSecret here"  

Step 4: Open the Startup.cs file and add the below code. We need to add this code inside the ConfigureServices(IServiceCollection services) method.

services.AddAuthentication().AddGoogle(googleOptions =>  
    googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];  
    googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];  

Step 5: Once added the code ConfigureServices(IServiceCollection services) method will look like below,

// This method gets called by the runtime. Use this method to add services to the container.  
        public void ConfigureServices(IServiceCollection services)  
            services.AddDbContext<ApplicationDbContext>(options =>  
            services.AddIdentity<ApplicationUser, IdentityRole>()  
            services.AddAuthentication().AddGoogle(googleOptions =>  
                googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];  
                googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];  
            // Add application services.  
            services.AddTransient<IEmailSender, EmailSender>();  

Step 6: That’s it, our application will be configured with Google Authentication.

Step 7: Press F5 to run the Application.

Step 8: Once the application launched in browser, Go to the Login Page. We can find the login link in Menu right corner.


Step 9: Login page will be opened. Here we will see the Google Login button.

Step 10: Click Google button will take you to the Google login page where you will be asked to fill in your Google credentials and authorize the Google app to use your Google account.

Step 11: After successful authentication from Google, you will be redirected to a registration page inside our application.

Step 12: We can register our email address by clicking Register button. Once registered, our email address will be shown at top right corner.


We have seen the steps to use your Google credentials to authenticate your ASP.NET Core web application. Other social network logins such as Facebook, Twitter, LinkedIn can also be achieved with ASP.NET Core web application.

He is a product manager at a reputed software company and a freelance blog writer. He is experienced in different technologies, web securities, and web applications. He keeps learning and make himself up to date on the latest technologies, news, health, and fitness. This encouraged him to share his experiences by writing articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
%d bloggers like this: