How to Create Google Login in asp.Net



Here we will see how to create google login using googleconnect and google API in dot net.

HTML CODE:
<div>
   <asp:Button ID="btnLogin" Text="Login" runat="server" OnClick="Login" />
   <asp:Panel ID="pnlProfile" runat="server" Visible="false">
   <hr />
   <table><tr><td rowspan="6" valign="top">
      <asp:Image ID="ProfileImage" runat="server" Width="81px" Height="96px" />
      </td></tr>
<tr><td>ID:<asp:Label ID="lblId" runat="server" Text=""></asp:Label>                    </td></tr>
       <tr><td>Name:<asp:Label ID="lblName" runat="server" Text=""></asp:Label>
       </td></tr>
       <tr><td>Email: <asp:Label ID="lblEmail" runat="server" Text=""></asp:Label>
       </td></tr>
       <tr><td>Gender:<asp:Label ID="lblGender" runat="server" Text=""></asp:Label>
       </td></tr>
       <tr><td>Type: <asp:Label ID="lblType" runat="server" Text=""></asp:Label>
       </td></tr>
       <tr><td><asp:Button ID="Button1" Text="Clear" runat="server" OnClick="Clear" />
       </td></tr>
    </table>
    </asp:Panel>
</div>

NAME SPACES
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
        {
            GoogleConnect.ClientId = "<Google Client ID>";
            GoogleConnect.ClientSecret = "<Google Client Secret>";
            GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];

            if (!string.IsNullOrEmpty(Request.QueryString["code"]))
            {
                string code = Request.QueryString["code"];
                string json = GoogleConnect.Fetch("me", code);
                GoogleProfile profile = new JavaScriptSerializer().Deserialize<GoogleProfile>(json);
                lblId.Text = profile.Id;
                lblName.Text = profile.DisplayName;
                lblEmail.Text = profile.Emails.Find(email => email.Type == "account").Value;
                lblGender.Text = profile.Gender;
                lblType.Text = profile.ObjectType;
                ProfileImage.ImageUrl = profile.Image.Url;
                pnlProfile.Visible = true;
                btnLogin.Enabled = false;
            }
            if (Request.QueryString["error"] == "access_denied")
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
            }
        }
        protected void Login(object sender, EventArgs e)
        {
            GoogleConnect.Authorize("profile", "email");
        }
        protected void Clear(object sender, EventArgs e)
        {
            GoogleConnect.Clear();
        }

        public class GoogleProfile
        {
            public string Id { get; set; }
            public string DisplayName { get; set; }
            public Image Image { get; set; }
            public List<Email> Emails { get; set; }
            public string Gender { get; set; }
            public string ObjectType { get; set; }
        }

        public class Email
        {
            public string Value { get; set; }
            public string Type { get; set; }
        }

        public class Image
        {
            public string Url { get; set; }
        }

Now check in your web browser and login.

Related Posts

Previous
Next Post »

Thanks for comments.....