Gmail login integration in asp.net application
source code(default.aspx)
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="google_login._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css">
.btngoogle
{
background-image:url(images/google+login+button.png);
border:1px solid white;
cursor:pointer;
}
</style>
<script type="text/javascript">
function showimage() {
var i = document.getElementById("imggoogle");
i.src = "images/google+logout+button.png";
i.style.border = "1px solid white";
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="loginform">
<div id="NotLoggedIn" runat="server">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Button ID="btnLoginToGoogle" runat="server" OnCommand="OpenLogin_Click"
ToolTip="Google_Login" CssClass="btngoogle"
CommandArgument="https://www.google.com/accounts/o8/id" Height="34px"
Width="143px" />
</td>
</tr>
</table>
<p />
<asp:Label runat="server" ID="lblAlertMsg" />
</div>
</div>
<asp:Label ID="lblname" runat="server"></asp:Label>
<asp:Label ID="lblemail" runat="server"></asp:Label>
<asp:Label ID="lblbirthdate" runat="server"></asp:Label>
<asp:Label ID="lblphone" runat="server"></asp:Label>
<asp:Label ID="lblgender" runat="server"></asp:Label>
<a id="btngmaillogout" runat="server" onserverclick="btngmaillogout_click">
<img src="http://accounts.google.com/logout" id="imggoogle" title="Google_LogOut" onerror="javascript:return showimage();" />
</a>
</asp:Content>
ccodebehind page(default.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using System.Web.Security;
using System.Net;
using System.IO;
namespace google_login
{
public partial class _Default : System.Web.UI.Page
{
OpenIdRelyingParty openid = new OpenIdRelyingParty();
protected void Page_Load(object sender, EventArgs e)
{
HandleOpenIDProviderResponse();
}
protected void HandleOpenIDProviderResponse()
{
var response = openid.GetResponse();
if (response != null)
{
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
NotLoggedIn.Visible
= false;
btngmaillogout.Visible = true;
var fetchResponse =
response.GetExtension<FetchResponse>();
Session["FetchResponse"]
= fetchResponse;
var response2 = Session["FetchResponse"] as FetchResponse;
lblemail.Text =
response2.GetAttributeValue(WellKnownAttributes.Contact.Email);
lblname.Text =
GetFullname(response2.GetAttributeValue(WellKnownAttributes.Name.First),response2.GetAttributeValue(WellKnownAttributes.Name.Last));
lblbirthdate.Text = response2.GetAttributeValue(WellKnownAttributes.BirthDate.WholeBirthDate);
lblphone.Text =
response2.GetAttributeValue(WellKnownAttributes.Contact.Phone.Mobile);
lblgender.Text
= response2.GetAttributeValue(WellKnownAttributes.Person.Gender);
break;
case AuthenticationStatus.Canceled:
lblAlertMsg.Text = "Cancelled.";
break;
case AuthenticationStatus.Failed:
lblAlertMsg.Text = "Login Failed.";
break;
}
}
else
{
return;
}
}
protected void OpenLogin_Click(object src, CommandEventArgs e)
{
string discoveryUri = e.CommandArgument.ToString();
var b = new UriBuilder(Request.Url) { Query = "" };
var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri);
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Person.Gender);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Phone.Mobile);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.BirthDate.WholeBirthDate);
req.AddExtension(fetchRequest);
req.RedirectToProvider();
}
private static string GetFullname(string first, string last)
{
var _first = first ?? "";
var _last = last ?? "";
if (string.IsNullOrEmpty(_first) || string.IsNullOrEmpty(_last))
return "";
return _first + " " + _last;
}
protected void btngmaillogout_click(object sender, EventArgs e)
{
lblemail.Text = "";
lblname.Text = "";
btngmaillogout.Visible = false;
Response.Redirect("~/Default.aspx");
}
}
}