Google Authentication With ASP.NET Core

Rainfall Software
Rainfall Software
Published in
2 min readJun 23, 2020

I just managed to get Google authentication working with my .NET Core app using the Microsoft.AspNetCore.Authentication package. There’s all sorts of security options that this package can handle, but I was just trying to do a simple Google sign in. It was a little fiddly to get working, so I’m writing it down here so that other people (as well as my future self) can find this information in a simple step-by-step.

Photo by Safar Safarov on Unsplash

Step One — Nuget Packages

You’ll need two Nuget packages to make this work:

Microsoft.AspNetCore.Authentication
Microsoft.AspNetCore.Authentication.Google

There are other packages to enable Facebook, Twitter, etc. sign ins, but I’m focusing on Google for now.

Step Two — Register Services

In your startup class, you’ll need to register the Authentication services that will run all of this. Here’s the code I used:

services.AddAuthentication(o =>
{
o.DefaultScheme = "Cookie";
o.DefaultChallengeScheme = "Cookie";
})
.AddCookie(o => o.LoginPath = "/")
.AddGoogle(o =>
{
o.ClientId = "MY_CLIENT_ID";
o.ClientSecret = "MY_CLIENT_SECRET";
});

This sets up authentication for the application, and sets up the Google provider that I got from the Google Cloud Console.

You might be asking yourself why I have the Cookie authentication as my default scheme. I’m only using Google for now, but I’m going to want to add more providers (Facebook, Twitter, etc.) to the application at a later date. With multiple providers, I don’t want to assume any one as the default so I’ve added a “dummy” scheme that will redirect users back to the home page (where my login prompt is) when they’re not signed in. If I used Google as the default, the user would be sent straight to the Google page if they’re not signed in.

Step Three — Create Sign In Action

In my Authentication controller, I created a simple action method that will redirect the user to the Google sign in page.

[Route("/auth/google")]
public IActionResult SignInGoogle()
{
return Challenge(new AuthenticationProperties
{
RedirectUri = Url.Action(nameof(SignInReturn))
}, GoogleDefaults.AuthenticationScheme);
}

This action sets up the URL that the user will be redirected to when they come back to my app, then returns a Challenge that will redirect to the sign in page on google.com.

Step Four — Create Return Action

Once Google has signed the user into my app, it redirects them to the URL I specified in the RedirectURI property in the challenge. I can do anything I need to in this method, possibly sending the user back to the original page they were trying to access.

[Route("/auth/return")]
public IActionResult SignInReturn()
{
// Do stuff with the user here. Their information is in the User
// property of the controller.
}

There’s more you could do here of course, but this is the minimum requirements for getting Google authentication working for my application.

--

--