Reader Level:
Articles

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

By Raj Kumar Beniwal on October 15, 2010
This article describes for you how to use the Google OpenID Authentication using DotNetOpenAuth in asp.net.
  • 1
  • 0
  • 28766
Download Files:
 


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.
 

Raj Kumar Beniwal

Raj Kumar Beniwal is two times Microsoft MVP(Client App Dev) and four times C# Corner MVP,  working as asst project manager with lots of hands on experience using ASP.NET 2.0/3.5, AJAX, MVC, C#, Visual Basic .NET, S... Read more

COMMENT USING

Trending up