Using Google Authentication with ASP.NET Core (ASP.NET 5)

** UPDATE – PLEASE READ **

Please visit: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins for updated instructions on how to use Google Authentication with ASP.NET Core.

The instructions in this article are out of date and I have not yet had a chance to update it.


While speaking with some local developers about ASP.NET Core I was asked if integrating Google Authentication was just as easy in ASP.NET Core, Core MVC (MVC6) as it was in the past. I honestly had not tested it yet so I decided to give it a shot. Thankfully, its very simple, but slightly different, so I’ve included detailed instructions below.

Note; this assumes you have Visual Studio 2015 Update 1 and ASP.NET 5 RC1 Installed. If not you can get both from the following links:

Create a new ASP.NET 5 MVC Project

Go to File -> New Project

1-vs-2015-new-project

Select ASP.net Web Application. Name your App and hit Ok

2-Web-Application-Name

Select Web Application under “ASP.NET 5” Templates

3-select-asp-net-5-web-application

Let Visual studio create the project and automatically restore the packages.

Once the solution loads, hit CTRL+F5 to run your app. Note the URL it runs on and copy this URL as we will use it later.

4-run-project-to-get-url

Setup and Create a new Google OAuth 2.0 Client ID and Secret

Head over to https://console.developers.google.com and sign in with your Google account

05-google-developer-console-home

In the top right click on the “Select a project” drop down and choose “Create a project”

06-google-developer-console-create-project

In the new project dialog that pops up enter a name for your project, agree to the terms and click “create”

07-google-developer-console-name-app

You will now be returned to your dashboard. Click on Enable and Manage API’s

08-enable-manage-apis

In the window that pops up choose the project you just created and hit continue

09-google-select-project

On the next page scroll down and click on Google+ API

10-select-google-plus-api

On the next screens click Enable and then Go to Credentials

11-select-enable

12-goto-credentials

On the Add Credentials screen choose Google+ API and Web server from drop downs. Choose User data under “What data will you be accessing” and click “What credentials do I need”

13-add-credentials

Remember how I had you save the url of your project? There is where we enter it. In the first box enter in the URL and make sure to remove the trailing / . In the second box add the same url but end it with /signin-google . Once you have done this click on Create client ID

14-add-credentials-filled-out

The next screen will let you customize the Google Consent page. This is the page the users see where they have to allow you to access their data. Just enter in a product name for now and click Continue

15-consent-screen

You are now done! Google only shows you the Client ID right away so I will show you how to go back and display your Client ID and Client Secret. First click Done on this screen

16-done-shows-clientid

The next screen shows all the credentials you have setup. Simply click on the first credential name.

17-apps-listed

This screen now shows your Client ID and Client Secret. Make note of both of these because we are going to use them in your project.

18-google-dev-console-shows-clientid-clientsecret

Setup your ASP.NET Core Application with Google Authentication

Now that we have the project setup and your Google Client ID and Secret, we simply need to modify the project to use Google Authentication.

First we need to install the Google Authentication Nuget package. To do this, let’s switch back over to Visual Studio and open up your project.json file. In the dependencies section add the following piece of code to the end.

It should look like this

19-add-google-authentication-package-project-json

Notice that when you do this, Visual Studio will automatically download and install the Nuget package setting up all your reference for you.

20-nuget-auto-restores-package

Now lets open up Startup.cs add add the following code. NOTE: It is important you add this code after app.UseIdentity(); and before app.UseMVC()

It should look like the following. Of course replace the values with your Client ID and Client Secret

21-edit-startup-add-google-auth

Let’s now run the project, CTRL + F5, and click login in the upper right hand corner. You should now see a page similar to the one below.

23-google-auth-consent-screen

Once the user hit’s allow they can authenticate using Google.

Technically we have done everything to get Google Authentication working. But, being I started from a brand new project I am going to continue and show you how to update the database use a migration so that the project will run properly.

Applying a database migration using DNX

If you followed this tutorial from the very beginning and started with a new project, you will get an error after hitting “Allow” similar to the one below.

24-ef-error

How to we fix this? Easy, open up a command prompt in the project folder. Run the command “dnx ef database update”. This will apply the initial database migration.

26-dnx-ef-update

Once you have done that, refresh your page and you are good to go! It should ask you to finish setting up your account and then log you into the site.

27-authentication-success

28-success-google-auth-works-asp-core

I hope you found this tutorial helpful! Happy Coding!

How Can I Learn MVC?

One of the best ways for any developer to grow is by staying up to date on all the current technologies and tools that are available to them. And as well all know, technology advances very fast.

The hardest part for most of us is simply finding the time to learn new things. If you are anything like me, you stick to what you are most familiar with to get a project done and out the door. In the end the client is still happy and your project still gets done, but, you may be missing out on ways you can streamline your work and get projects done even faster and with cleaner code.

So where am I going with this?

Well, for years and years I have developed in VB.net using web forms. I have literally built over 300 sites using ASP.net Web Forms and LINQ to SQL. For quite some time know I had read about MVC and even had a few colleagues demonstrate it to me. While I was impressed I really never took the time to sit down and learn it. After all, it was a different way of thinking and who has time to learn? And let’s be honest, I was just being stubborn and did not want to leave my comfort zone.

Well after months and months of bugging me, a friend finally got me to sit down and take a few courses on MVC, AngularJs, and Web API’s. Needless to say, once I started learning about it I was absolutely blow away and have not looked back since.

The biggest challenge is finding a good resource online to provide great training that would serve as a jump off point for me. I found most of my training at Microsoft Virtual Academy and Plural Sight.

I’ve included links below to the courses that I found useful and hope that they help you as well.

Note; Microsoft Virtual Academy is 100% free. Plural Sign has a minimal monthly fee.

Enjoy and Happy Coding!