How to use Google OpenID Authentication using DotNetOpenAuth in ASP.NET



This article describes for you how to use the Google OpenID Authentication using DotNetOpenAuth in ASP.Net.

Download the DotNetOpenAuth from here. First of all make a new website and add a reference to DotNetOpenAuth.dll.

Drag and drop a button on page.

<asp:Button ID="btnLoginToGoogle" Runat="server" Text="Google Login" OnCommand="OpenLogin_Click" 
                    CommandArgument="https://www.google.com/accounts/o8/id"
                Font-Bold="True" Font-Italic="True" Width="162px" /> 

using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty; 
 
protected void Page_Load(object sender, EventArgs e)
    {
        OpenIdRelyingParty OIDRP = new OpenIdRelyingParty();
        var str = OIDRP.GetResponse();
        if (str != null)
        {
            switch (str.Status)
            {
                case AuthenticationStatus.Authenticated:
                    NotLoggedIn.Visible = false;
                    Session["GoogleIdentifier"] = str.ClaimedIdentifier.ToString();
                    Response.Redirect("Home.aspx");
                    break;
                case AuthenticationStatus.Canceled:
                    lblMessage.Text = "Cancelled.";
                    break;
                case AuthenticationStatus.Failed:
                    lblMessage.Text = "Login Failed.";
                    break;
            }
        } 
    }

  protected void OpenLogin_Click(object src, CommandEventArgs e)
    {
        string str = e.CommandArgument.ToString();
        OpenIdRelyingParty openid = new OpenIdRelyingParty();
        var b = new UriBuilder(Request.Url) { Query = "" };
        var req = openid.CreateRequest(str, b.Uri, b.Uri);
        req.RedirectToProvider();
    } 

Now run the website.
1.JPG

Image 1

When you click on Login button.

2.JPG

Image2.

Insert google email and password.

3.JPG

Image3.
 

Up Next
    Ebook Download
    View all
    Learn
    View all