The main objective of this article is:
- Implementing Google Authentication
Install Google Authentication NuGet Package:
Let's install the package required for google authentication.
Visual Studio 2022:
Install-Package Microsoft.AspNetCore.Authentication.Google -Version 7.0.3
Visual Studio Code:
dotnet add package Microsoft.AspNetCore.Authentication.Google --version 7.0.3
Register With Google:
We need to register our Razor Page application with Google to enable Google authentication. So following are the registration steps with Google.
(Step 1)
Go to Google API & Service could console at
"https://console.cloud.google.com/projectselector2/apis/dashboard?supportedpurview=project"
"https://console.cloud.google.com/projectselector2/apis/dashboard?supportedpurview=project"
(Step 2)
Click on the 'Create Project' button.
(Step 3)
Give a project name and then click on the 'Create' button.
(Step 4)On creating a project successfully, we can observe it as a selected project in our dashboard like below.
(Step 5)
Select 'OAuth Consent Screen' left side menu, next 'User Type' => 'External' then click on 'Create' button.
(Step 6) Need to configure App under the project
Give an email at Developer Contact Information and then click the 'Save And Continue' button.
(Step 7)
In 'Scopes' click on 'Add OR Remove Scopes' and then select 'email', 'profile', 'openid' scopes and click on 'Save and Continue' button
(Step 8)
Now add the email addresses of users to test google authentication with our application
(Step 9) In the 'Summary' page click on the 'Back To Dashboard' page.
(Step 10)
Select 'Credentials' left menu then click on the 'Create Credentials' button, and then select 'OAuth Client Id'
(Step 11)Now Configure 'Create OAuth Client'.
Application Type => Web Application, Enter a name in the text box.
Now in 'Authorized Redire URL's' let's add URL which has our application domain end with the default path like 'signin-google'
(Step 12)
Now save 'Client Id' & 'Client Secret', we are going to use them in our application.
Configure the Google Secret Keys:
Now let's configure 'Client Id' & 'Client Secret' in our 'appsetting.Development.json'.
appsetting.Development.json:
"GoogleAuthSettings": { "ClientId": "xxxxxxxxxxxxxxxxxxxxxxx", "ClientSecret": "xxxxxxxxxxxxxxxxxxxxxxxx" }
Register Google Auth Service:
Now let's register the google auth service in our 'Program.cs'
Program.cs:
builder.Services.AddAuthentication().AddGoogle(googleOptions => { googleOptions.ClientId = builder.Configuration.GetSection("GoogleAuthSettings") .GetValue<string>("ClientId"); googleOptions.ClientSecret = builder.Configuration.GetSection("GoogleAuthSettings") .GetValue<string>("ClientSecret"); });
Test Google Authentication Flow:
(Step 1) On enabling the google authentication service we can see a 'Google' button on both 'Login' and 'Registration' pages
(Step 2) On clicking the 'Google' button we will redirect to the google login page.
(Step 3) Next it explicitly asks us to associate the google email with our application.
(Step 4) On clicking 'Register' button we can observe user information added to 'AspNetUser' and 'AspNetUserLogin' tables
(Step 5) After email confirmation, if we try again to login, we can observe we get authenticated as below.In the next article, we are going to implement Facebook Login with help of AspNet Core Identity.Hopefully, I think this article delivered some useful information on the Asp.Net Core Identity In Razor Pages. using I love to have your feedback, suggestions, and better techniques in the comment section below
Refer:
Part-2 | Asp.Net Core Identity Series[.NET 7] | Registration Email Confirmation
Part-3 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Email
Part-3 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Email
Part-4 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Phone
Comments
Post a Comment