Google Authentication in an ASP.NET Core Web API

This is an example of how to set up Google authentication on an ASP.NET Core Web API.

It takes a default ASP.NET Core Web API application, and uses the Microsoft.AspNetCore.Authentication.Google plugin to set up Google authentication.

Subsequently, this plugin allows you to input the credentials generated that you get when setting up a Google Cloud Console service.

The credentials provided are a client ID and secret, and these need to be provided into the ASP.NET Core Web API, so Google knows which service to authenticate against.

To test it, the Privacy Policy page has been protected and requires authentication.

When a user visits the Privacy Policy page, it redirects to Google's log in page.

Assuming the authentication with Google is successful, the app redirects to a page to show the claims that are returned from Google to the Web API.

Software

This is the software that will need to be installed onto your machine.

  • Visual Studio 2019. Version 16.6.4 or above. It will work with the free community version.
  • .NET 3.1.6 SDK. Other SDK's above this may work, but this has not been tried out.

Get The Application Working

These are the steps to get the application working.

  • Fill out the Download the Source Code form. We will send you an email where you can download the source code.
  • Set up a service in Google Cloud Console. The service will generate a client ID and secret that you will need. Read our How to Add Google Authentication to a ASP.NET Core Application article for full details on how to do this.
  • Input the ClientId from the Google Cloud Console service into the line 38 of RoundTheCode.GoogleAuthentication/Startup.cs from the source code.
  • Input the ClientSecret from the Google Cloud Console service into the line 39 of RoundTheCode.GoogleAuthentication/Startup.cs from the source code.

From there:

  • Start the application in Visual Studio, and navigate to https://localhost:8888 in your favourite web browser.
  • Click on the Privacy Policy page, and you should be redirected to Google's login page.
  • Log in with your Google account, and you should be redirected back to the app. It should display all the claims that have been returned from Google.

Watch The Video

If you are unsure on how to get the application working, you can watch our live stream that we did on Google Authentication.

We created a new project in Google Cloud Platform, set up Google authentication in an ASP.NET Core application and watch the Google authentication in action, including the claims that are exposed from Google.