avatarM. Ramadhan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

8952

Abstract

Console for the production app with its production redirect URI. <i>(<a href="https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-7.0">https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social</a>)</i></p></blockquote><ul><li>Click the CREATE button to make an OAuth client.</li></ul><figure id="ad45"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6S_NC3ohXNpQPmTTVv3ZJw.png"><figcaption><b>Figure 10 </b>Client ID and Secret created</figcaption></figure><ul><li>Download the JSON file for use in the application's configuration. Listing 1 shows its content.</li><li>Click the <b>OK</b> button.</li></ul><figure id="f5ab"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*4JK39sxZNantOaKkBd_S5w.png"><figcaption><b>Figure 11 </b>Client ID and Secret created</figcaption></figure><p id="769d"><b>Listing 1</b> The contents of the <code>client_….googleusercontent.com.json</code> file</p><div id="8ea4"><pre><span class="hljs-punctuation">{</span> <span class="hljs-attr">"web"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"client_id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"684XXXXXf2n62.apps.googleusercontent.com"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"project_id"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"book-project-367314"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"auth_uri"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"https://accounts.google.com/o/oauth2/auth"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"token_uri"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"https://oauth2.googleapis.com/token"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"auth_provider_x509_cert_url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"https://www.googleapis.com/oauth2/v1/certs"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"client_secret"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"GOCXXXXX3s2-"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"redirect_uris"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> <span class="hljs-string">"https://localhost:44333/signin-google"</span> <span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span> <span class="hljs-punctuation">}</span></pre></div><p id="2d29"><b>Note</b> Your client_id and client_secret should be different from mine in Listing 1 above.</p><h1 id="7786">Install Google Authentication Middleware</h1><p id="a374">We need to install the <code>Microsoft.AspNetCore.Authentication.Google</code> NuGet package in our project.</p><p id="5126">This project is a continuation of the <i>Blazor Server Project #15</i>.</p><div id="bc08" class="link-block"> <a href="https://readmedium.com/blazor-server-project-15-role-based-authorization-717fa1065cca"> <div> <div> <h2>Blazor Server Project #15: Role-based Authorization</h2> <div><h3>A practical security guide: add role services, specify & implement role-based authorization, and assign roles to users…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*XhTR11M8Q3XDY-juAL723Q.png)"></div> </div> </div> </a> </div><p id="6250">If you don't have the project, you can download it via the link below.</p><div id="2f8a" class="link-block"> <a href="https://github.com/emramadhan/BlazorServerP15"> <div> <div> <h2>GitHub - emramadhan/BlazorServerP15</h2> <div><h3>You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*NUpYnm-VUxfAC0Fr)"></div> </div> </div> </a> </div><ul><li>Open the project in Visual Studio 2022.</li><li>Select <b>Tools > NuGet Package Manager > Manage NuGet Packages Solution...</b> from the menu, and the <i>NuGet Solution</i> dialog appears.</li></ul><figure id="4f57"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QUdisZBCODe7DOnPiI0yPQ.png"><figcaption><b>Figure 12 </b>Installing Google Authentication middleware</figcaption></figure><ul><li>Click <b>Browse</b>.</li><li>In the <b>Search</b> box, type <code>Microsoft.AspNetCore.Authentication.Google</code>.</li><li>Select <b>Microsoft.AspNetCore.Authentication.Google</b> from the package list, check checkboxes and click <b>Install</b>.</li></ul><figure id="5052"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*eKeHd1ZY4Kv3UGS6RifVyg.png"><figcaption><b>Figure 13</b> Confirmation dialog of installing Google Authentication middleware</figcaption></figure><ul><li>Click <b>OK</b> to proceed with the installation.</li></ul><figure id="0f56"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*7H_EvVi9EFvpIA6VVjTaKQ.png"><figcaption><b>Figure 14</b> The license acceptance</figcaption></figure><ul><li>Click <b>I Accept </b>to accept the license.</li></ul><h1 id="9ad0">Store the Google Client ID and Secret</h1><ul><li>Right-click the <b>BookApp</b> project in the Solution Explorer pane.</li></ul><figure id="35ff"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pkNlGRdw893Mf654wLCiCw.png"><figcaption><b>Figure 15</b> Selecting menus to manage User Secrets</figcaption></figure><ul><li>From the context menu, click <b>Manage User Secrets</b> to open the <code>secrets.json</code> file.</li><li>Please copy the code below and paste it into the file.</li></ul><p id="02bd"><b>Listing 2</b> The contents of the <code>secrets.json</code> file</p><div id="0a02"><pre><span class="hljs-punctuation">{</span> <span class="hljs-attr">"Authentication:Google:ClientId"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"<client-id>"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"Authentication:Google:ClientSecret"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"<client-secret>"</span> <span class="hljs-punctuation">}</span></pre></div><ul><li>Replace <code><client-id></code> and <code><client-secret></code><b> </b>with yours.</li><li>Save the file. Visual Studio saves it in the file system path: <code>%APPDATA%\Microsoft\UserSecrets&lt;user_secrets_id>\secrets.json</code></li></ul><h1 id="ec51">Configure Google Authentication</h1><p id="1c8c">Add the following code into <code>Startup.cs</code> file to configure Google Authentication.</p><p id="7d42"><b>Listing 3</b> The contents of the modified <code>Startup.cs</code> file</p><div id="a898"><pre>... <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ConfigureServices</span>(<span class="hljs-params">IServiceCollection services</span>) ... <span class="hljs-comment">//Google Authentication service </span> services.<span class="hljs-title">AddAuthentication</span>().<span class="hljs-title">AddGoogle</span>(<span class="hljs-params">googleOptions => { googleOptions.ClientId = Configuration[<span class="hljs-string">"Authentication:Google:ClientId"</span>]; googleOptions.ClientSecret = Configuration[<span class="hljs-string">"Authentication:Google:ClientSecret"</span>]; }</span>)</span>; ... ...</pre></div><p id="0963">The application uses the <code>AddGoogle()</code> method to read the <code>ClientId</code> and <code>ClientSecret</code> from the <code>secrets.json</code> file.</p><h1 id="61bd">Sign in with Google</h1><p id="8543">See Table 1. We will test login using Google accounts for three user types: ▸Unregistered user ▸Registered user with no role ▸Registered user with a "User" role and check menu list compliance with user authorization.</p><figure id="9e5e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ytQKbSrd-esOZmJbYfmjYg.png"><figcaption>Table 1 Specification of menu authorization</figcaption></figure><p id="dd05">We first modify the code in the <code>Login.cshtml</code> and <code>Register.cshtml</code> files to make the UI more informative.</p><p id="8f9b"><b>Listing 4</b> The contents

Options

of the modified <code>Login.cshtml</code> file</p><div id="54b8"><pre>... <span class="hljs-meta">@foreach</span> (<span class="hljs-keyword">var</span> provider <span class="hljs-keyword">in</span> <span class="hljs-title class_">Model</span>.<span class="hljs-property">ExternalLogins</span>) { <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">...</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Log in using your @provider.DisplayName account"</span>></span> Log in with @provider.DisplayName <span class="hljs-tag"></<span class="hljs-name">button</span>></span></span> } ...</pre></div><p id="93f1"><b>Listing 5</b> The contents of the modified <code>Register.cshtml</code> file</p><div id="5f79"><pre>... <span class="hljs-meta">@foreach</span> (<span class="hljs-keyword">var</span> provider <span class="hljs-keyword">in</span> <span class="hljs-title class_">Model</span>.<span class="hljs-property">ExternalLogins</span>) { <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">...</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Register using your @provider.DisplayName account"</span>></span> Register with @provider.DisplayName <span class="hljs-tag"></<span class="hljs-name">button</span>></span></span> } ...</pre></div><h2 id="bf08">Unregistered users</h2><p id="41b9">Users can log in using their Google account even if they are not registered. There are two options for unregistered users to sign in, register, and set the password. The options are: (1) via the <b>Log in </b>link, or (2) via the <b>Register </b>link.</p><h2 id="c5a9">Via the Log in link</h2><ul><li>Run the app, click <b>Login</b> on the homepage and follow the steps in Figure 16.</li></ul><figure id="a61e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6oBZi9G9KYYTP3oWwHRZEw.png"><figcaption><b>Figure 16</b> Sequence for logging in and registering</figcaption></figure><ul><li>An unregistered user does registration on sequence ⑤ and confirms on sequences ⑥. Figure 17 shows the list of users before and after registering a new user, and Figure 18 displays the data before and after confirming registration.</li><li>An unregistered user does registration on sequence ⑤ and confirms on sequences ⑥. Figure 17 shows the data before and after registration, and Figure 18 displays the data before and after confirmation.</li></ul><figure id="b8d0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_jlLgrwcdSSFF2j7rg_KAA.png"><figcaption></figcaption></figure><figure id="535d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8dcwuLDwyiGwphJQ7h2YCA.png"><figcaption><b>Figure 17 </b>Left: list of old users Right: after registering a new user</figcaption></figure><figure id="5d67"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OAw8aYDHKuw8nPsUcLtaaA.png"><figcaption></figcaption></figure><figure id="fe61"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Rx4qgWWctBKPOsv7giQOCw.png"><figcaption><b>Figure 18</b> Before (left) and after (right) confirming registration</figcaption></figure><ul><li>The new user has no password yet; the password hash is NULL. Follow the next steps in Figure 19.</li></ul><figure id="1ba8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*H0TlK3-ILbLYFsd0HFgLYQ.png"><figcaption><b>Figure 19</b> Sequence for setting the password</figcaption></figure><ul><li>The new user sets the password on sequence ⑫. Figure 20 shows the data before and after setting the password.</li></ul><figure id="c864"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*mf6ri5t_0oIRaVa92vgPmw.png"><figcaption></figcaption></figure><figure id="5c26"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*kcGtOWIYRDfLw9X4W_x-HQ.png"><figcaption><b>Figure 20</b> Before (left) and after (right) setting the password</figcaption></figure><h2 id="5424">Via the Register link</h2><ul><li>Run the app, click <b>Register</b> on the homepage and follow the steps in Figure 21.</li></ul><figure id="6760"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1RXD3erEVXqL0N2gdWxsTg.png"><figcaption><b>Figure 21</b> Sequence for registering and logging in</figcaption></figure><ul><li>An unregistered user does registration and confirmation on sequences ⑤ and ⑥.</li><li>The new user has no password yet. Follow the next steps in Figure 19.</li></ul><h2 id="617d">Registered users with no role</h2><p id="28b9">Now the two users above are registered with no role. We test one of them.</p><ul><li>Run the app, click <b>Login</b> on the homepage and follow the steps in Figure 22.</li></ul><figure id="318a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nmIkVsMSUyT2I7_gnzRPNA.png"><figcaption><b>Figure 22 </b>Sequence of logging in for registered user with no role</figcaption></figure><h2 id="77be">Registered users with a "User" role</h2><ul><li>We must first assign a "User" role to a user. Copy the <i>Id</i> of <i>[email protected]</i> from the <i>AspNetUsers</i> table and the <i>Id</i> of the <i>User</i> role from the <i>AspNetRole</i> table and paste them into the <i>AspNetUserRoles</i> table. See Figure 23.</li></ul><figure id="4f31"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*t5AIvoJBsfAJBE6SV7Uq2Q.png"><figcaption><b>Figure 23</b> List of users and their roles</figcaption></figure><ul><li>Run the app, click <b>Login</b> on the homepage and follow the steps in Figure 24.</li></ul><figure id="1341"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*J426cHieVOVwZNYo_5bZiw.png"><figcaption><b>Figure 24</b> Sequence of logging in for registered user with "User" role</figcaption></figure><p id="f148">The three types of users above log in using their Google accounts and have different menu authorizations according to the specifications in Table 1.</p><ul><li>An unregistered user can only access two menu items, <i>Home </i>and <i>Books</i>. See sequence ① in Figures 16, 21, 22, and 24 and sequence ⑮ in Figure 19.</li><li>A registered user with no role can access three menu items, <i>Home</i>, <i>Books</i>, and <i>Publishers</i>. See sequences ⑩ and ⑭ in Figure 19 and sequence ⑤ in Figure 22.</li><li>A registered user with a "User" role can access four menu items, <i>Home</i>, <i>Books</i>, <i>Publishers, </i>and <i>Authors</i>. See sequences ⑤ in Figure 24.</li></ul><p id="b62b">What about pages and elements authorization? Please check their compliance with the specifications in the <a href="https://readmedium.com/blazor-server-project-15-role-based-authorization-717fa1065cca#923a">previous post</a>.</p><h1 id="b7ac">Summary</h1><p id="200a">By integrating Google Sign-In into your web app using Google OAuth 2.0, any user — unregistered users, registered users with no role, and registered users with roles — can sign in using their Google accounts. Their authorization is compliant with the specifications in the app.</p><p id="beb5">Hopefully beneficial. Your feedback will be precious.</p><h1 id="57bc">References</h1><div id="f54f" class="link-block"> <a href="https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-6.0"> <div> <div> <h2>Google external login setup in ASP.NET Core</h2> <div><h3>By Valeriy Novytskyy and Rick Anderson. This tutorial shows you how to enable users to sign in with their Google account…</h3></div> <div><p>learn.microsoft.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*tWkj-ThfdoXT8i_G)"></div> </div> </div> </a> </div><div id="98cd" class="link-block"> <a href="https://developers.google.com/identity/sign-in/web/sign-in"> <div> <div> <h2>Integrating Google Sign-In into your web app | Google Sign-In for Websites | Google Developers</h2> <div><h3>Google Sign-In manages the OAuth 2.0 flow and token lifecycle, simplifying your integration with Google APIs. A user…</h3></div> <div><p>developers.google.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*zGxHrgvdhwAHREDC)"></div> </div> </div> </a> </div></article></body>

Blazor Server Project #16: How to Implement Google Authentication

A step-by-step guide to integrating Google Sign-In into your web app using OAuth 2.0, admitting users to log in using their Google account

Table of Contents

OverviewCreate the Google OAuth 2.0 Client ID and SecretInstall Google Authentication MiddlewareStore the Google Client ID and SecretConfigure Google AuthenticationSign in with GoogleUnregistered usersRegistered users with no roleRegistered users with a "User" roleSummaryReferences

Photo by Kai Wenzel on Unsplash

This article is the sixteenth in a series covering the Blazor Server Project: (1) How to create a CRUD operation using Dapper (2) Building a dropdown list involves a 1:N relationship (3) How to implement a checkbox list involving an M:N relationships (4) Understanding URL routing and navigation (5) Creating and using page layout (6) How to create a reusable modal dialog component (7) Practical guide to making a master-detail page (8) Master-detail page using dynamic query (9) How to avoid SQL injection attacks (10) Hiding/showing HTML elements (11) Migrate to ASP.NET Core 6.0 (12) Installing ASP.NET Core Identity (13) Integrating Identity tables into the existing project database (14) Authentication and authorization (15) Role-based authorization (16) How to implement Google Authentication (17) Facebook Authentication and Authorization (18) How to configure Twitter Authentication (19) How to Customize Password Policy (20) Account Lockout Policy

These articles are for anyone who wants to learn how to build Blazor Server applications in a practical approach through some sample projects. It will be straightforward if you have some experience with C#, HTML, CSS, and SQL.

Overview

In the Blazor Server Project #14 article, we implemented authentication using ASP.NET Core Identity. For now, we cover how to apply Google Authentication by integrating Google Sign-In into your web app using Google OAuth 2.0. So, users can log in to your web using their Google accounts. The steps are: ▸ Create the Google OAuth 2.0 Client ID and Secret. ▸ Install Google Authentication Middleware. ▸ Store the Google Client ID and Secret. ▸ Configure Google Authentication. ▸ Sign in with Google.

Sign In With Google helps you to quickly and easily manage user authentication and sign-in to your website. Users sign into a Google Account, provide their consent, and securely share their profile information with your platform. (https://developers.google.com/identity/gsi/web/guides/overview)

Create the Google OAuth 2.0 Client ID and Secret

  • Go to the Credentials page. — Choose your account. — Enter your password. The following page appears.
Figure 1 Credentials page
  • Click CREATE PROJECT button, and the page below occurs.
Figure 2 Creating Book Project
  • Enter the Project name: Book Project, click CREATE, and you will see the project dashboard below.
Figure 3 Book Project dashboard
  • Click CONFIGURE CONSENT SCREEN button to display the OAuth consent screen.
Figure 4 OAuth consent screen
  • Select User Type: External > CREATE, and the following Edit app registration page appears.
Figure 5 Edit app registration page
  • In the App information dialog, provide an app name: BookApp, user support email, and developer contact information. Click SAVE AND CONTINUE button.
  • Step through the Scopes step. Click SAVE AND CONTINUE button.
  • Step through the Test users step. Click SAVE AND CONTINUE button.
  • Review the OAuth consent screen.
Figure 6 OAuth consent screen of Book Project
  • Go back to the app Dashboard.
Figure 7 Book Project dashboard
  • In the Credentials tab, click CREATE CREDENTIALS > OAuth client ID.
Figure 8 Page of creating OAuth client ID
  • Select Application type > Web application, and name your OAuth client, e.g. BookApp client.
Figure 9 Creating OAuth client ID for Book Project
  • In the Authorized redirect URIs section, select ADD URI. For testing, you can specify URIs that refer to the local machine, https://localhost:44333/signin-google where 44333 is the app's port. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses.

When deploying the site, either: Update the app’s redirect URI in the Google Console to the app’s deployed redirect URI. Create a new Google API registration in the Google Console for the production app with its production redirect URI. (https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social)

  • Click the CREATE button to make an OAuth client.
Figure 10 Client ID and Secret created
  • Download the JSON file for use in the application's configuration. Listing 1 shows its content.
  • Click the OK button.
Figure 11 Client ID and Secret created

Listing 1 The contents of the client_….googleusercontent.com.json file

{
   "web": {
      "client_id": "684XXXXXf2n62.apps.googleusercontent.com",
      "project_id": "book-project-367314",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_secret": "GOCXXXXX3s2-",
      "redirect_uris": [ "https://localhost:44333/signin-google" ]
   }
}

Note Your client_id and client_secret should be different from mine in Listing 1 above.

Install Google Authentication Middleware

We need to install the Microsoft.AspNetCore.Authentication.Google NuGet package in our project.

This project is a continuation of the Blazor Server Project #15.

If you don't have the project, you can download it via the link below.

  • Open the project in Visual Studio 2022.
  • Select Tools > NuGet Package Manager > Manage NuGet Packages Solution... from the menu, and the NuGet Solution dialog appears.
Figure 12 Installing Google Authentication middleware
  • Click Browse.
  • In the Search box, type Microsoft.AspNetCore.Authentication.Google.
  • Select Microsoft.AspNetCore.Authentication.Google from the package list, check checkboxes and click Install.
Figure 13 Confirmation dialog of installing Google Authentication middleware
  • Click OK to proceed with the installation.
Figure 14 The license acceptance
  • Click I Accept to accept the license.

Store the Google Client ID and Secret

  • Right-click the BookApp project in the Solution Explorer pane.
Figure 15 Selecting menus to manage User Secrets
  • From the context menu, click Manage User Secrets to open the secrets.json file.
  • Please copy the code below and paste it into the file.

Listing 2 The contents of the secrets.json file

{
  "Authentication:Google:ClientId": "<client-id>",
  "Authentication:Google:ClientSecret": "<client-secret>"
}
  • Replace <client-id> and <client-secret> with yours.
  • Save the file. Visual Studio saves it in the file system path: %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json

Configure Google Authentication

Add the following code into Startup.cs file to configure Google Authentication.

Listing 3 The contents of the modified Startup.cs file

...
   public void ConfigureServices(IServiceCollection services)
   ...
      //Google Authentication service         
      services.AddAuthentication().AddGoogle(googleOptions =>
      {
         googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
         googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
      });
  ...
...

The application uses the AddGoogle() method to read the ClientId and ClientSecret from the secrets.json file.

Sign in with Google

See Table 1. We will test login using Google accounts for three user types: ▸Unregistered user ▸Registered user with no role ▸Registered user with a "User" role and check menu list compliance with user authorization.

Table 1 Specification of menu authorization

We first modify the code in the Login.cshtml and Register.cshtml files to make the UI more informative.

Listing 4 The contents of the modified Login.cshtml file

...
   @foreach (var provider in Model.ExternalLogins)
   {
      <button type="submit"
         ...
         title="Log in using your @provider.DisplayName account">
         Log in with @provider.DisplayName
      </button>
   }
...

Listing 5 The contents of the modified Register.cshtml file

...
   @foreach (var provider in Model.ExternalLogins)
   {
      <button type="submit"
         ...
         title="Register using your @provider.DisplayName account">
         Register with @provider.DisplayName
      </button>
   }
...

Unregistered users

Users can log in using their Google account even if they are not registered. There are two options for unregistered users to sign in, register, and set the password. The options are: (1) via the Log in link, or (2) via the Register link.

Via the Log in link

  • Run the app, click Login on the homepage and follow the steps in Figure 16.
Figure 16 Sequence for logging in and registering
  • An unregistered user does registration on sequence ⑤ and confirms on sequences ⑥. Figure 17 shows the list of users before and after registering a new user, and Figure 18 displays the data before and after confirming registration.
  • An unregistered user does registration on sequence ⑤ and confirms on sequences ⑥. Figure 17 shows the data before and after registration, and Figure 18 displays the data before and after confirmation.
Figure 17 Left: list of old users Right: after registering a new user
Figure 18 Before (left) and after (right) confirming registration
  • The new user has no password yet; the password hash is NULL. Follow the next steps in Figure 19.
Figure 19 Sequence for setting the password
  • The new user sets the password on sequence ⑫. Figure 20 shows the data before and after setting the password.
Figure 20 Before (left) and after (right) setting the password

Via the Register link

  • Run the app, click Register on the homepage and follow the steps in Figure 21.
Figure 21 Sequence for registering and logging in
  • An unregistered user does registration and confirmation on sequences ⑤ and ⑥.
  • The new user has no password yet. Follow the next steps in Figure 19.

Registered users with no role

Now the two users above are registered with no role. We test one of them.

  • Run the app, click Login on the homepage and follow the steps in Figure 22.
Figure 22 Sequence of logging in for registered user with no role

Registered users with a "User" role

  • We must first assign a "User" role to a user. Copy the Id of [email protected] from the AspNetUsers table and the Id of the User role from the AspNetRole table and paste them into the AspNetUserRoles table. See Figure 23.
Figure 23 List of users and their roles
  • Run the app, click Login on the homepage and follow the steps in Figure 24.
Figure 24 Sequence of logging in for registered user with "User" role

The three types of users above log in using their Google accounts and have different menu authorizations according to the specifications in Table 1.

  • An unregistered user can only access two menu items, Home and Books. See sequence ① in Figures 16, 21, 22, and 24 and sequence ⑮ in Figure 19.
  • A registered user with no role can access three menu items, Home, Books, and Publishers. See sequences ⑩ and ⑭ in Figure 19 and sequence ⑤ in Figure 22.
  • A registered user with a "User" role can access four menu items, Home, Books, Publishers, and Authors. See sequences ⑤ in Figure 24.

What about pages and elements authorization? Please check their compliance with the specifications in the previous post.

Summary

By integrating Google Sign-In into your web app using Google OAuth 2.0, any user — unregistered users, registered users with no role, and registered users with roles — can sign in using their Google accounts. Their authorization is compliant with the specifications in the app.

Hopefully beneficial. Your feedback will be precious.

References

Blazor
Authentication
Authorization
Google
Oauth
Recommended from ReadMedium