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.
A quick rundown of what I have done
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.
How to use it
Well, to use it is pretty straight forward. Here are the steps:
1. First of all you will need a Client ID and Client Secret and for that you need to register an app in the Google Cloud Console. I am not going to run through this process step by step, so please refer to the Google documentation for that. You can also refer to that previous blog post of mine for a more step-by-step walk through. That blog post described how to do it in Google API Console, which is now replaced with the Google Cloud Console, but it should be similar enough to follow.
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:
2. Next you need to install the NuGet package I published into your project:
3. Lastly alter your Startup.Auth.cs file to enable the Google+ authentication provider.
app.UseGooglePlusAuthentication("YOUR CLIENT ID ", "YOUR CLIENT SECRET");
Source code and how to contribute
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.