arrow_back ASP.NET Core - Implement Google One Tap Sign In
Thanks for the help and I hope that you can guide me with what I have.
in my index page:
<div id="g_id_onload"
data-client_id="???"
data-login_uri='/google-response'>
</div>
in my controller page:
[Route("google-response")]
public async Task<ActionResult> GoogleResponse()
{
var google_csrf_name = "g_csrf_token";
var cookie = Request.Cookies[google_csrf_name];
if (cookie == null)
return StatusCode((int)HttpStatusCode.BadRequest);
var requestbody = Request.Form[google_csrf_name];
if (requestbody != cookie)
return StatusCode((int)HttpStatusCode.BadRequest);
var idtoken = Request.Form["credential"];
GoogleJsonWebSignature.Payload payload = await GoogleJsonWebSignature.ValidateAsync(idtoken).ConfigureAwait(false);
TempData["name"] = payload.Name;
TempData["email"] = payload.Email;
return Json(payload);
}
in appsettingsjson:
"GoogleAuthSettings": {
"ClientId": "???",
"ClientSecret": "???"
}
I added the following packages:
Google.Apis.Auth and Microsoft.AspNetCore.Authentication.Google
I try to run the above but nothing is showing.
Can you help. Thanks
person Raymond access_time 3 months ago
Re: ASP.NET Core - Implement Google One Tap Sign In
Hi Rami,
About Razor Page, I recommend you follow this official page to understand the details. For each page, it usually contains one .cs (PageModel
) class file and also one .cshtml file.
About ApplicationUser
and other identity related classes, they are included as part of ASP.NET Core Identity. You can follow this page to understand more: Introduction to Identity on ASP.NET Core | Microsoft Learn. For the example I provided, it utilizes Identity to create users or update users. However to integrate your web application with Google One Tap Sign-in, you don't necessarily need to use Identity and you can create your own identity systems to manage your website's users.
About Callback part: once user clicks the prompt to sign in with Google and if the authentication is successful, Google will make a POST HTTP call to the endpoint you provided. In the POST request, a CSFR token and credential will be provided. Through the credential, you can get user's identity information from Google, for example, first name, last name, email addresses, etc.
new ApplicationUser { UserName = payload.Email, Email = payload.Email, FirstName = payload.GivenName, LastName = payload.FamilyName, IsEnabled = true, EmailConfirmed = true, DateRegister = DateTime.Now };
The information is included in the response payload after validating the credential successfully:
GoogleJsonWebSignature.Payload payload = await GoogleJsonWebSignature.ValidateAsync(idToken);
The above code uses an OAuth package published by Google: Class GoogleJsonWebSignature (1.60.0) | .NET client library | Google Cloud.
To learn more about how this whole flow works, I suggest you reading more about OAuth if you have not worked on it previously.
Hi Rami,
About Razor Page, I recommend you follow this official page to understand the details. For each page, it usually contains one .cs (PageModel
) class file and also one .cshtml file.
About ApplicationUser
and other identity related classes, they are included as part of ASP.NET Core Identity. You can follow this page to understand more: Introduction to Identity on ASP.NET Core | Microsoft Learn. For the example I provided, it utilizes Identity to create users or update users. However to integrate your web application with Google One Tap Sign-in, you don't necessarily need to use Identity and you can create your own identity systems to manage your website's users.
About Callback part: once user clicks the prompt to sign in with Google and if the authentication is successful, Google will make a POST HTTP call to the endpoint you provided. In the POST request, a CSFR token and credential will be provided. Through the credential, you can get user's identity information from Google, for example, first name, last name, email addresses, etc.
new ApplicationUser { UserName = payload.Email, Email = payload.Email, FirstName = payload.GivenName, LastName = payload.FamilyName, IsEnabled = true, EmailConfirmed = true, DateRegister = DateTime.Now };
The information is included in the response payload after validating the credential successfully:
GoogleJsonWebSignature.Payload payload = await GoogleJsonWebSignature.ValidateAsync(idToken);
The above code uses an OAuth package published by Google: Class GoogleJsonWebSignature (1.60.0) | .NET client library | Google Cloud.
To learn more about how this whole flow works, I suggest you reading more about OAuth if you have not worked on it previously.
person Rami Taher access_time 3 months ago
Re: ASP.NET Core - Implement Google One Tap Sign In
Dear Raymond,
Thanks for the reply. I have some questions. The razer page is it a class or just a razor file? Where do i put this file in the project? the last sample code, you mentioned that:
ApplicationUser class is inherited from IdentityUser. Fields _signInManager and _userManager are the instances of Identity SignInManager and UserManager respectively.
I am new to asp.net so I am not sure how to use them because when i took your code it gave me errors. Lastly what is a callback handeler and how call it and where to save it in the project?
Thanks again for the help in advance.
Dear Raymond,
Thanks for the reply. I have some questions. The razer page is it a class or just a razor file? Where do i put this file in the project? the last sample code, you mentioned that:
ApplicationUser class is inherited from IdentityUser. Fields _signInManager and _userManager are the instances of Identity SignInManager and UserManager respectively.
I am new to asp.net so I am not sure how to use them because when i took your code it gave me errors. Lastly what is a callback handeler and how call it and where to save it in the project?
Thanks again for the help in advance.
person Raymond access_time 3 months ago
Re: ASP.NET Core - Implement Google One Tap Sign In
Hi Rami,
I don't have time to publish a video at the moment. Do you have any specific questions about the steps mentioned above?
Hi Rami,
I don't have time to publish a video at the moment. Do you have any specific questions about the steps mentioned above?
person Rami Taher access_time 3 months ago
Re: ASP.NET Core - Implement Google One Tap Sign In
Hi,
Do you have a video explaining how to do that? I need it for my project
Hi,
Do you have a video explaining how to do that? I need it for my project
Did you register your local dev application in Google Cloud?
data-client_id
is the Client Id of the OAuth you registered.person Rami Taher access_time 3 months ago
Re: ASP.NET Core - Implement Google One Tap Sign In
Thanks for the help and I hope that you can guide me with what I have.
in my index page:
in my controller page:
in appsettingsjson:
I added the following packages:
Google.Apis.Auth and Microsoft.AspNetCore.Authentication.Google
I try to run the above but nothing is showing.
Can you help. Thanks