Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
12 Mar
2022

Google Sign In using GoogleAuthentication Nuget package in Asp.Net MVC

by Shubham Batra

423

Download Attachments

GoogleAuthentication Nuget Package

GoogleAuthentication  package can be install from Nuget packages and it is supported in .Net MVC and .net Core. It helps you to integrate google signin in three easy steps and you will be able to check the data retunred from google.For installing this you have to follow the steps

You can also install this nuget package using the command in package manager console 

Install-Package GoogleAuthentication -Version 1.0.0

After you have successfully installed GoogleAuthentication pakage . You have to go to the controller and add this namespace on your controller.



using GoogleAuthentication.Services;

Now, we will see how to use that in the code to get GoogleLogin working.


  public ActionResult Index()
        {
            var ClientID = "Enter client id here";
            var url = "https://localhost:44375/Login/GoogleLoginCallback";
            var response = GoogleAuth.GetAuthUrl(ClientID, url);
            ViewBag.response = response;
            return View();
        }

So , this will be the action which will be called for showing the Google Sign In button. Here , we need to pass the Client Id and redirect url to our function and it will return us a Url which we have to pass in the href of Login anchor tag as discussed in next step. For getting the Client Id and setting Redirect Url you can check out detailed steps in How to get Client Id and register Redirect Url . 

 var response = GoogleAuth.GetAuthUrl(ClientID, url);
 ViewBag.response = response;

Here, we are passing client id and url to GetAuthUrl and this will return url which we setting in ViewBag and will be used in next step. Now on the view you have to add one button like this


<a class="btn btn-default" href="@ViewBag.response">Goggle Log in</a>

Now we have to creating a new action method to catch the redirect url and must match the name of the redirect url action. So we will add this new action

  public async Task<ActionResult> GoogleLoginCallback(string code)
        {
            try
            {
                var ClientSecret = "Enter Client Secret here";
                var ClientID = "Enter Client Id here";
                var url = "https://localhost:44375/Login/GoogleLoginCallback";
                var token = await GoogleAuth.GetAuthAccessToken(code, ClientID, ClientSecret, url);
                var userProfile = await GoogleAuth.GetProfileResponseAsync(token.AccessToken.ToString());
                var googleUser = JsonConvert.DeserializeObject<GoogleProfile>(userProfile);
               
            }
            catch (Exception ex)
            {
                
            }
            return RedirectToAction("index", "Users");
        }

Code- This will be the parameter that we received from google containing a code that is used to obtain token in next steps.

ClientId- This must be the Client Id which we have used in first step

ClientSecret- ClientSecret can be obtained from the same place from where you get the ClientId.

Url- This is the same redirect url that we have used in first step.

var token = await GoogleAuth.GetAuthAccessToken(code, ClientID, ClientSecret, url);

This method will take the parameters explained above and will return you a access token which we be used in next step to obtain profile info.

  var userProfile = await GoogleAuth.GetProfileResponseAsync(token.AccessToken.ToString());

This method will take the access token from previous method and return back the profile data for the user that we have logged in . This will be in json format. You can check the returned data format in next image

For searializing this json , you can create this class model.

 public class GoogleProfile
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Picture { get; set; }
        public string Email { get; set; }
        public string Verified_Email { get; set; }
        public string MobilePhone { get; set; }
        public string Locale { get; set; }
    }

Now , you can deseiralize the json data using our model And then use the data anywhere you need.

 var googleUser = JsonConvert.DeserializeObject<GoogleProfile>(userProfile);

This is how we can  integrate Google Sign In using GoogleAuthentication Nuget package in Asp.Net MVC  using some easy steps. If you face any issue don't forget to add comments .

  • |
  • Google Sign In , Google Login , Social Login , Google Login in AspNet MVC , GoogleAuthentication Nuget package

Comments

Tags

How to set Date and time format in IIS Manager
IIS Manager
IIS
Internet Information Services (IIS) Manager
Internet Information Services (IIS)
Internet Information Services
Error Handling In AspNet Core
Exception Handling Asp Net Core
Exception Handling
Exception Handling Asp Net
Aspnet
Creating Log Files in MVC
Error Handling in MVC
Exception Handling in AspNet
Handling Exceptions and Creating Error Logs in Asp net Mvc using base controller
net
Code2Tonight
Stopping Browser Reload On Save
Repository Pattern with ADONet in MVC
Repository Pattern With ASPNET MVC And AdoNet
MVC Crud Operation
Jquery Full Calender Integrated With ASPNET
Full Calendar
Jquery Calendar
Slick Slider
Slick Slider Example
responsive carousels
Entity Framework
MVC
Intergrate SummerNote Text Editor into AspNet MVC
Web Config
Auto Redirection
Redirection from Http to https
AspNet
Url Rewriting
Postgre
PgAdmin4
PostgreSql
A Non Fatal Error Occured During Cluster Initialisation In Postgre SQL
Microsoft Outlook
Outlook Appointments
Microsoft Exchange Service
Send Email With HTML Template And PDF Using ASPNet C#
Send Email
Email with html template
email with pdf attachment
email with html and pdf
Microsoft Outlook Contacts
Outlook
Microsoft Exhchange Service
JSON
Convert string with dot notation to JSON
HTTP Error 5025 ANCM Out Of Process Startup Failure
Internet Information Service
Net core
Payumoney Integration With AspNet MVC
Prism js
Highlighting Syntax
Syntax Highlighting
code stylings
c#
Jquery AJax
Ajax
Jquery
Implement Stripe Payment Gateway In ASPNET
Using Checkout in an ASPNET Web Forms application
Stripe Payment
Stripe Payment Integration
Stripe Integeration
How to upload Image file using AJAX andjQuery
upload Image file using AJAX and jquery
Ajax call
file upload using ajax
file uploading using ajax and jquery
ConfigurationBuilder does not contain a definition for SetBasePath
Reading app json file in dot net core
Appsetting jso
Dot Net Core
Globalization and localization in ASPNET Core
Asp Net Core with Resource file resx
How to get the resx file strings in asp net core
Culture in Net core
Localisation in AspNet Core
Url Encryption in AspNet MVC
Url Encryption in C#
Url Encryption
Custom Helpers
Slick Slider with single slide
Slick
Vue js
Child Components
How to reload vue js child components
Net Core
Visual Studio
C#
Net core 31
Razor
Zoom sdk
Zoom c# wrapper Inegration
zoom Integration in c#
Zoom Integration
Zoom window sdk
vue js toggle button
vue js
toggle buttons
vuejs
vue js toggle switch
SignalR
VueJs
SignalR in Net Core
Chat App in Vue js
Chat App using SignalR
AspNet Chat app
JPlayer
Html5 Audio Video Player
Music Player
QR Code Generator
QR Code
Jquery QR Code
AspNet MVC
Google Maps
Google map api
Places API
Google map Places API in AspNet
Jquery Autocomplete
Autocomplete
Jquery UI Autocomplete
ExcelDataReader
Import data from excel in AspNet
Card Number Formatting
Amex Card Format
Card Format
FCM
Cloud Messaging
Android Notifications
FCM Notifications for IOS
IOS Notifications
Angular js
apply css on child components in Angular js
Angular Mentions
Google Sign In
Google Login
Google Oauth Api
Social Login
Aspnet Mvc
Google + Api
Create and publish a package using Visual Studio (NET Framework
Windows)
Create and publish a nuget package
create your own nuget package
Image compress
Image optimization
compress Image
optimize Image
WebForm
AspNet Web Pages
Batch Script
Database backup
Powershell
ASpNet
Sql Server Backup
LinkedIn Authentication
Login using LinkedIN
Social Login in AspNet
LinkedIn Authentication in AspNet MVC
LinkedIn Login in aspnet MVC
Shuffle List in c#
C#Net
Google Login in AspNet MVC
GoogleAuthentication Nuget package
Password Encryption
RFC Encryption
Encryption and Decryption
Encryption in AspNet
Base 64 Encryption
Base 64 Decryption

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Kurukshetra
  • [email protected]

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2022 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Json Beautifier