Add Google Login Support In ASP.NET With OpenID

18. February 2012 17:12

API ASP.NET  10 Comments

Few years back I blogged about adding OpenID login support in ASP.NET application. This time I am blogging about adding Google login support in ASP.NET application. A friend of mine is trying to integrate multiple third party authentication support for one of the application he is developing for his client. He is using DotNetOpenAuth for Google authentication. the code I am using here is from my friend and I am sharing it with his explicit permission.

First, download the latest version of DotNetOpenAuth and add its reference in your web application and these two namespaces.

using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;

After adding the reference, add a normal button with CommandArgument to point

<asp:Button ID="btnGoogleLogin" runat="server" CommandArgument="" 
        OnCommand="btnGoogleLogin_Click" Text="Sign In with Google" />

On the button click event on the server side:

protected void btnGoogleLogin_Click(object sender, CommandEventArgs e)
    string discoveryUri = e.CommandArgument.ToString();
    OpenIdRelyingParty openid = new OpenIdRelyingParty();
    var URIbuilder = new UriBuilder(Request.Url) { Query = "" };
    var req = openid.CreateRequest(discoveryUri, URIbuilder.Uri, URIbuilder.Uri);

Now when you click the button it will take you to Google login page which look something like this.

You can see on the right side of the screen with the information of the site requesting the authentication. Once you get successfully authenticated with your entered user name and password, you will then redirect to the confirmation page:

As I am using my local development server, you will see Locahost. Once you deploy the application in the production environment it will automatically get the name of the domain. Clicking on the Sign in button you will then be redirected to the main page, but before you get to the main page you need to check whether the authentication was successful or was it cancelled by the user or was failed. To make sure use the below code on the load event of the login page:

protected void Page_Load(object sender, EventArgs e)
    OpenIdRelyingParty rp = new OpenIdRelyingParty();
    var response = rp.GetResponse();
    if (response != null)
        switch (response.Status)
            case AuthenticationStatus.Authenticated:
                Session["GoogleIdentifier"] = response.ClaimedIdentifier.ToString();
            case AuthenticationStatus.Canceled:
                Session["GoogleIdentifier"] = "Cancelled.";
            case AuthenticationStatus.Failed:
                Session["GoogleIdentifier"] = "Login Failed.";


On Default.aspx page I have set the ClaimedIdentifier:

The response/status returned from Google will be checked here and we will redirect the application to work the way accordingly. My friend sends me the above code to find out whether there is any way we can logout from the service. Well, unfortunately there isn't any specific way to log out using DotNetOpenAuth? But there is a workaround. I don't know if it is a good practice or bad but it worked out for me. To logout, I am just calling this logout URL used by Google.

If you have some suggestions or you know a better way or approach of doing this then please drop a line in the comments sections.

Currently rated 4.2 by 9 people

Comments (10)

Kane Kane
3/10/2012 5:37:03 PM #

I think your title is incorrect, and should reference OpenId not OAuth. If you are interested in the differences refer to these two links: and

prashant prashant
3/10/2012 10:51:42 PM #

You are absolutely right Kane!! Thanks for the information. I have changed the title now.

4/25/2012 6:33:20 PM #

5/5/2012 7:56:40 PM #

I don't see how this helps you. How does your application know WHO the person is? All you have is the google claimed identifier. How do you translate that into an email address so you can identify who that person is and give them specific access to your site?

prashant prashant
5/5/2012 9:49:49 PM #

I have skipped that part but if you want to get the details of the user read here:

Ajay kumar Ajay kumar
7/12/2012 6:20:43 PM #

hi, nice post but how will get user details except email, name, country i.e Mobile No thanks.

Prashant Prashant
7/12/2012 9:00:46 PM #

Glad you like it!! You can get the details of the user, but the approach is slightly different. Please read here for more information:

anil babu anil babu
7/16/2012 4:59:19 PM #

I am getting an error,e.CommandArgument.ToString(); directory missing

Prashant Prashant
7/16/2012 6:14:49 PM #

Anil Babu: If you check the code above carefully you will see that I have changed the button auto-generated code from: [b]protected void btnGoogleLogin_Click(object sender, EventArgs e)[/b] to[b] protected void btnGoogleLogin_Click(object sender, CommandEventArgs e)[/b]. There is a CommandEventArgs instead of EventArgs and this is because I am sending a command by setting CommandArgument property in button. This will solve your problem. :)

Upniwesh Kumar Tiwari Upniwesh Kumar Tiwari
3/16/2016 2:12:57 PM #

Previous url ( was working but now it is throwing "No OpenID endpoint found." exception.

Add Comment

Visit to discover Indian blogs Computers Blogs