Login With Google Account API In ASP.NET And Get Google Plus Profile Details In C#

Here, I have explained how to log in with Google Plus Account API in ASP.Net Website using C#

After the account is verified, the user’s Google Plus Profile detail such as Google Plus Profile ID, Google Plus Profile Image, Name, and Gender are fetched and displayed on the website.

Step 1 - go to Google Dashboard

https://console.cloud.google.com/apis/dashboard

Step-2

Create a New Application as follows and click it

Click on red circle and open the following dialog

Click on + button,  open the  following page and give the application a name. I will give my application name as connect

Click on create button and select bell icon and then a project

Select APIs &Services->Credentials

Click on Create Credentials button and select Oath client ID option

Click on Configure consent Screen and Configure your Domain. In my case, I am using localhost and my domain is http://localhost:52523/


Click on Save button. Select web application and give Authorized redirect URIs

"For use with requests from a web server. This is the path in your application that users are redirected to after they have authenticated with Google. The path will be appended to the authorization code for access. Must have a protocol. Cannot contain URL fragments or relative paths. Cannot be a public IP address."

My website redirect URI is http://localhost:52523/index.aspx in my website user transfer index.aspx page after successful login


Click on Create button. Get your client ID and Client Secret and copy it and click ok

Click on Library link


Search Google+ API and click on it

Click on Enable button and Enable Google+ API Services

Open Visual Studio

Create new Asp.net Website application

Add Webform login.aspx and Add following code

login.aspx page

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title>login</title>  
  7.         <style type="text/css">  
  8.             .gmailbutton {  
  9.                 background-color: #ff0000;  
  10.                 color: white;  
  11.                 width: 150px;  
  12.             }  
  13.         </style>  
  14.     </head>  
  15.   
  16.     <body>  
  17.         <form id="form1" runat="server">  
  18.             <div>  
  19.                 <asp:Button ID="btnlogin" runat="server" Text="Login With Gmail" CssClass="gmailbutton" /> </div>  
  20.         </form>  
  21.     </body>  
  22.   
  23.     </html>  

 

login.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class login: System.Web.UI.Page {  
  8.     protected void Page_Load(object sender, EventArgs e) {}  
  9.     protected void btnlogin_Click(object sender, EventArgs e) {  
  10.         //your client id  
  11.         string clientid = "XXXXXXXXXXXXXXXXXXXX";  
  12.         //your client secret  
  13.         string clientsecret = "XXXXXXXXXXXXXXXXXX";  
  14.         //your redirection url  
  15.         string redirection_url = "XXXXXXXXXXXXXXXX";  
  16.         string url = "https://accounts.google.com/o/oauth2/v2/auth?scope=profile&include_granted_scopes=true&redirect_uri=" + redirection_url + "&response_type=code&client_id=" + clientid + "";  
  17.         Response.Redirect(url);  
  18.     }  
  19. }  
Create index.aspx web form When user Successful login by gmail then user redirect to index.aspx page

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <table>  
  13.                     <tr>  
  14.                         <td>  
  15.                             <asp:Image ID="imgprofile" runat="server" Height="100px" Width="100px" /> </td>  
  16.                     </tr>  
  17.                     <tr>  
  18.                         <td> Id </td>  
  19.                         <td>  
  20.                             <asp:Label ID="lblid" runat="server" Text=""></asp:Label>  
  21.                         </td>  
  22.                     </tr>  
  23.                     <tr>  
  24.                         <td> Name </td>  
  25.                         <td>  
  26.                             <asp:Label ID="lblname" runat="server" Text=""></asp:Label>  
  27.                         </td>  
  28.                     </tr>  
  29.                     <tr>  
  30.                         <td> Gender </td>  
  31.                         <td>  
  32.                             <asp:Label ID="lblgender" runat="server" Text=""></asp:Label>  
  33.                         </td>  
  34.                     </tr>  
  35.                     <tr>  
  36.                         <td> locale </td>  
  37.                         <td>  
  38.                             <asp:Label ID="lbllocale" runat="server" Text=""></asp:Label>  
  39.                         </td>  
  40.                     </tr>  
  41.                     <tr>  
  42.                         <td> link </td>  
  43.                         <td>  
  44.                             <asp:HyperLink ID="hylprofile" runat="server">Profile link</asp:HyperLink>  
  45.                         </td>  
  46.                     </tr>  
  47.                 </table>  
  48.             </div>  
  49.         </form>  
  50.     </body>  
  51.   
  52.     </html>  

index.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Text;  
  7. using System.Web;  
  8. using System.Web.Script.Serialization;  
  9. using System.Web.UI;  
  10. using System.Web.UI.WebControls;  
  11. public partial class index: System.Web.UI.Page {  
  12.     //your client id  
  13.     string clientid = "XXXXXXXXXXXXXXXXXXXX";  
  14.     //your client secret  
  15.     string clientsecret = "XXXXXXXXXXXXXXXXXX";  
  16.     //your redirection url  
  17.     string redirection_url = "XXXXXXXXXXXXXXXX";  
  18.     string url = "https://accounts.google.com/o/oauth2/token";  
  19.     public class Tokenclass {  
  20.         public string access_token {  
  21.             get;  
  22.             set;  
  23.         }  
  24.         public string token_type {  
  25.             get;  
  26.             set;  
  27.         }  
  28.         public int expires_in {  
  29.             get;  
  30.             set;  
  31.         }  
  32.         public string refresh_token {  
  33.             get;  
  34.             set;  
  35.         }  
  36.     }  
  37.     public class Userclass {  
  38.         public string id {  
  39.             get;  
  40.             set;  
  41.         }  
  42.         public string name {  
  43.             get;  
  44.             set;  
  45.         }  
  46.         public string given_name {  
  47.             get;  
  48.             set;  
  49.         }  
  50.         public string family_name {  
  51.             get;  
  52.             set;  
  53.         }  
  54.         public string link {  
  55.             get;  
  56.             set;  
  57.         }  
  58.         public string picture {  
  59.             get;  
  60.             set;  
  61.         }  
  62.         public string gender {  
  63.             get;  
  64.             set;  
  65.         }  
  66.         public string locale {  
  67.             get;  
  68.             set;  
  69.         }  
  70.     }  
  71.     protected void Page_Load(object sender, EventArgs e) {  
  72.         if (!IsPostBack) {  
  73.             if (Request.QueryString["code"] != null) {  
  74.                 GetToken(Request.QueryString["code"].ToString());  
  75.             }  
  76.         }  
  77.     }  
  78.     public void GetToken(string code) {  
  79.         string poststring = "grant_type=authorization_code&code=" + code + "&client_id=" + clientid + "&client_secret=" + clientsecret + "&redirect_uri=" + redirection_url + "";  
  80.         var request = (HttpWebRequest) WebRequest.Create(url);  
  81.         request.ContentType = "application/x-www-form-urlencoded";  
  82.         request.Method = "POST";  
  83.         UTF8Encoding utfenc = new UTF8Encoding();  
  84.         byte[] bytes = utfenc.GetBytes(poststring);  
  85.         Stream outputstream = null;  
  86.         try {  
  87.             request.ContentLength = bytes.Length;  
  88.             outputstream = request.GetRequestStream();  
  89.             outputstream.Write(bytes, 0, bytes.Length);  
  90.         } catch {}  
  91.         var response = (HttpWebResponse) request.GetResponse();  
  92.         var streamReader = new StreamReader(response.GetResponseStream());  
  93.         string responseFromServer = streamReader.ReadToEnd();  
  94.         JavaScriptSerializer js = new JavaScriptSerializer();  
  95.         Tokenclass obj = js.Deserialize < Tokenclass > (responseFromServer);  
  96.         GetuserProfile(obj.access_token);  
  97.     }  
  98.     public void GetuserProfile(string accesstoken) {  
  99.         string url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + accesstoken + "";  
  100.         WebRequest request = WebRequest.Create(url);  
  101.         request.Credentials = CredentialCache.DefaultCredentials;  
  102.         WebResponse response = request.GetResponse();  
  103.         Stream dataStream = response.GetResponseStream();  
  104.         StreamReader reader = new StreamReader(dataStream);  
  105.         string responseFromServer = reader.ReadToEnd();  
  106.         reader.Close();  
  107.         response.Close();  
  108.         JavaScriptSerializer js = new JavaScriptSerializer();  
  109.         Userclass userinfo = js.Deserialize < Userclass > (responseFromServer);  
  110.         imgprofile.ImageUrl = userinfo.picture;  
  111.         lblid.Text = userinfo.id;  
  112.         lblgender.Text = userinfo.gender;  
  113.         lbllocale.Text = userinfo.locale;  
  114.         lblname.Text = userinfo.name;  
  115.         hylprofile.NavigateUrl = userinfo.link;  
  116.     }  
  117. }  

Now, press F5 build And run web site

Click on login with gmail button


Display User profile