Introduction
Prerequisites
- Install the latest .NET Core 3.0 Preview SDK from here.
- Install the latest preview of Visual Studio 2019 from here.
- Install ASP.NET Core Blazor Language Services extension from here.
Get the source code from GitHub.
Create Server Side Blazor Application
- Click on “Create a new project”.
- Select “ASP.NET Core Web Application” from available project types. Click on Next.
- A new “Configure your new project” screen will open. Put the name of the project as BlazorGoogleAuth and click Create.
- In the next screen, select “.NET Core” and “ASP.NET Core 3.0” from dropdowns on the top left.
- Select “Blazor (server-side)” from the list of available templates.
- Click on Change Authentication button, a “Change Authentication” dialog box will open. Select “Individual User Account” and click OK. Click on Create button to create the application.
These steps are shown in the GIF image below.

Before running the application, we need to apply migrations to our app. Navigate to Tools >> NuGet Package Manager >> Package Manager Console. It will open the Package Manager Console. Put in Update-Database command and hit enter. This will update the database using Entity Framework Code First Migrations. Refer to the image below.


Create a Google API Console project
Navigate to here. Login with your Google account. Follow the steps mentioned below.
- Click on “Configure a Project” button.
- A “Configure a project for Google Sign-in” dialog box will open asking you to select or create a new project.
- Select “Create a new project” from the dropdown. Name your project “BlazorAuthDemo” and click on Next.
- In the “Configure your OAuth client” screen, put your product name. You can use any name of your choice. Here we will put “BlazorAuth” as the product name.
- In the next screen, select “Web server” from the “Where are you calling from” dropdown.
- It will then ask you to put the “Authorized redirect URIs”. Give the base URL of your application with "/signin-google " appended to it. For this tutorial, the URL will be "https://localhost:44327/signin-google".
- Click on Create. The dialog box will now prompt you with the client ID and client secret. Make a note of ClientId and ClientSecret field. We will need these values to configure Google authentication in our web app
Refer to the GIF below for a better understanding.

- Do not use the word “Google” in your product name. You will be prompted with an error and you will not be allowed to create the app. This means “BlazorGoogleAuth” is an invalid project name.
- Project names must be between 4 and 30 characters and may only contain letters, numbers, spaces, and hyphens.
Installing Google authentication middleware NuGet package
Open here. Select the version of .NET Core 3 from the “Version History”. Copy the command from the “package manager” tab. Run this command in the NuGet package manager console of our application.
For this application, we are using .NET Core 3.0.0-preview6.19307.2. Therefore, we will run the following command in the package manager console of our application.

Configure the server-side Blazor app to use Google authentication
Open our web application once again and Right-click the project in the Solution Explorer. Select Manage User Secrets from the context menu. A secrets.json file will open. Put the following code in it.
- {
- "Authentication:Google:ClientId": "Your Google ClientId here",
- "Authentication:Google:ClientSecret": "Your Google ClientSecret here"
- }
- services.AddAuthentication().AddGoogle(googleOptions =>
- {
- googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
- googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
- });
Adding authorization to Blazor pages
- @using Microsoft.AspNetCore.Authorization
- @attribute [Authorize]
Execution Demo
Refer to the GIF below for a better understanding.

Conclusion
Please get the source code from GitHub and play around to get a better understanding. You can read my other articles on my blog.
- Authentication And Authorization With Facebook In Server-Side Blazor
- CRUD With Blazor Using Google Cloud Firestore
- Deploying A Blazor Application On Firebase
- Creating A Reusable Grid Component For Blazor
- Blazor - Publishing A Component To NuGet Gallery
- Single Page Application Using Server-Side Blazor

Ihor LazarenkoPosted Jun 27, 2019, 5:12 AM
Does it work in IE?