how to sinc gmail to your webpage in asp.net

How to Integrate Gmail Login into your ASP.Net Website

Google supports the OpenID 2.0 protocol, providing authentication support as an OpenID provider. On request from a third-party site, Google authenticates users who are signing in with an existing Google account, and returns to the third-party site an identifier that the site can use to recognize the user. This identifier is consistent, enabling the third-party site to recognize the user across multiple sessions.
Note: OpenID authentication now supports Google Apps (hosted) accounts. 

OpenID authentication process

OpenID login authentication for web applications involves a sequence of interactions between your web application, Google's login authentication service, and the end user.

 

Working with OpenID

1. The web application asks the end user to log in by offering a set of log-in options, including using their Google account.
2. Login by credentials.
 3. The web application sends a "discovery" request to Google to get information on the Google login authentication endpoint.

4. Google returns an XRDS document, which contains the endpoint address and then web application sends a login authentication request to the Google endpoint address.

5. Once logged in, Google displays a confirmation page and notifies the user that a third-party application is requesting authentication. Page ask for allow approval and don't approval.


6. If the user approves the authentication, Google returns the user details.

7. If you want to retrieve the user's details of currently logged in google user, then use OpenAuth/OpenID. For .Net, a library is available to do all this task, DotNetOpenAuth. Download it, extract it and add in your bin folder of application.

Source Code:

Add code in your login.aspx page:
<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: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" />
  <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>
 Add code in login.aspx.cs page
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
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)
        {
           // logout from gmail and return to website default/home page    
            Response.Redirect("~/Default.aspx");        
        }

Comments

Popular posts from this blog

How whatsapp works

ABOUT WHATSAPP

Edit Java Class file at runtime