Categories
Technical

Google Authentication in ASP.NET Core Application

Introduction

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,

https://karthikek.home.blog/2019/04/15/how-to-setup-google-developer-console

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.

https://karthikek.home.blog/2019/04/12/how-to-create-asp-net-core-web-application

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 =>  
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));  
            services.AddIdentity<ApplicationUser, IdentityRole>()  
                .AddEntityFrameworkStores<ApplicationDbContext>()  
                .AddDefaultTokenProviders();  
            services.AddAuthentication().AddGoogle(googleOptions =>  
            {  
                googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];  
                googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];  
            });  
            // Add application services.  
            services.AddTransient<IEmailSender, EmailSender>();  
            services.AddMvc();  
        }  

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.

http://localhost:61364/Account/Login

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.

Conclusion

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.

Leave a Reply

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