A few months ago I wrote a blog post on how to add a Google+ sign in for ASP.NET MVC 4. With the release ASP.NET MVC 5 the whole authentication system has been overhauled and the ASP.NET team has introduced ASP.NET Identity, which has built-in support for external authentication providers. The list of external authentication providers which are included out of the box includes Google, but this is based on OpenID and not OAuth, which is fine if all you want to use it for is authentication. Sadly however this is problematic if you want to use the user’s authentication token to make calls to the Google+ API - for that you will need a Google+ OAuth provider. Since no one else seems to have taken up the call to develop a Google+ OAuth provider for ASP.NET Identity, I decided to see what I can do.
I like the path of least resistance, and do not suffer too badly from the “not invented here“ syndrome. The code for the Google+ OAuth provider I wrote is almost an exact copy of the code developed by the OWIN team for the standard Facebook OAuth provider. I do not claim any level of cleverness or credit for this code, other than shamelessly copying it and adapting it to work with Google+
The original work from Microsoft is located at http://katanaproject.codeplex.com/. As it is open source I will see what I can do to integrate my work into theirs. For now I just wanted to get something out which people can use.
This blog post is actually a combination of a bunch of stuff I did in previous blog posts. As noted above there was absolutely no genius involved on my side as far as the development of the actual OWIN OAuth provider goes. I copied the work which Microsoft had done on the Facebook provider and adapted it for Google+. Having done a LOT of OAuth work over the past year I understand more or less how the whole OAuth process works (I think). I have walked through the Google+ OAuth sign in process in a previous blog post so if you have a need to understand that please go and read that blog post. Also have a look at Google’s own documentation on how the OAuth 2.0 flow works.
To give the button the Google+ look and feel, you can refer to my blog post on Pretty social login buttons for ASP.NET MVC 5.
Last month I wrote a blog post on how to access the Twitter OAuth token, and I used the same technique in the demo project to store and retrieve the Google+ OAuth token. I then use this token on the home page to extract the Google+ user information and do a raw dump of the JSON output. It is not pretty but it should give you a good enough idea of how to store and extract the OAuth token and then use that to call the Google+ API.
Well, to use it is pretty straight forward. Here are the steps:
Please note that you will need to register your Redirect URI in Google Cloud Console to point to the URL signin-googleplus, as displayed in the screenshot below:
app.UseGooglePlusAuthentication("YOUR CLIENT ID ", "YOUR CLIENT SECRET");
This was a quick job and works in my testing. You can refer to the source code located at https://github.com/beabigrockstar/GooglePlusAuthProvider to look at the demo project. If you have any issues with the provider please feel free to fork the project and submit a pull request.