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
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title>login</title>
- <style type="text/css">
- .gmailbutton {
- background-color: #ff0000;
- color: white;
- width: 150px;
- }
- </style>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="btnlogin" runat="server" Text="Login With Gmail" CssClass="gmailbutton" /> </div>
- </form>
- </body>
-
- </html>
login.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class login: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {}
- protected void btnlogin_Click(object sender, EventArgs e) {
-
- string clientid = "XXXXXXXXXXXXXXXXXXXX";
-
- string clientsecret = "XXXXXXXXXXXXXXXXXX";
-
- string redirection_url = "XXXXXXXXXXXXXXXX";
- 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 + "";
- Response.Redirect(url);
- }
- }
Create index.aspx web form When user Successful login by gmail then user redirect to index.aspx page
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <asp:Image ID="imgprofile" runat="server" Height="100px" Width="100px" /> </td>
- </tr>
- <tr>
- <td> Id </td>
- <td>
- <asp:Label ID="lblid" runat="server" Text=""></asp:Label>
- </td>
- </tr>
- <tr>
- <td> Name </td>
- <td>
- <asp:Label ID="lblname" runat="server" Text=""></asp:Label>
- </td>
- </tr>
- <tr>
- <td> Gender </td>
- <td>
- <asp:Label ID="lblgender" runat="server" Text=""></asp:Label>
- </td>
- </tr>
- <tr>
- <td> locale </td>
- <td>
- <asp:Label ID="lbllocale" runat="server" Text=""></asp:Label>
- </td>
- </tr>
- <tr>
- <td> link </td>
- <td>
- <asp:HyperLink ID="hylprofile" runat="server">Profile link</asp:HyperLink>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
-
- </html>
index.aspx.cs
Now, press F5 build And run web site
Click on login with gmail button
Display User profile