arrow_back ASP.NET Core - Implement Google One Tap Sign In

comment Comments
Raymond Raymond #1849 access_time 8 months ago more_vert

Did you register your local dev application in Google Cloud? data-client_id is the Client Id of the OAuth you registered.

format_quote

person Rami Taher access_time 8 months ago

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

RT Rami Taher Wahdan #1848 access_time 8 months ago more_vert

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

format_quote

person Raymond access_time 8 months ago

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.

Raymond Raymond #1847 access_time 8 months ago more_vert

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.

format_quote

person Rami Taher access_time 8 months ago

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.

RT Rami Taher Wahdan #1846 access_time 8 months ago more_vert

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.

format_quote

person Raymond access_time 8 months ago

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?

Raymond Raymond #1845 access_time 8 months ago more_vert

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?

format_quote

person Rami Taher access_time 8 months ago

Hi,


Do you have a video explaining how to do that? I need it for my project

RT Rami Taher Wahdan #1844 access_time 8 months ago more_vert

Hi,


Do you have a video explaining how to do that? I need it for my project

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts