In a previous post, I described my experience working with Facebook Login integration. I recently implemented Google login integration (aka Google+ Sign-In), which is very similar to Facebook's integration. This feature enables Google users to login to your site using their Google credentials. As such, the user does not need to create and remember a username and password for your site. Their Google credentials are used instead.
Here is the main page on Google's site regarding Google+ Sign-In: https://developers.google.com/+/web/signin/. The majority of the information is very straight-forward. However, I will share a few pieces of code that pulled it all together for me.
Here is my signinCallback function code, which redirects the browser window upon successful Google login. Note that the accessToken parameter is sent to our server for further processing.
function signinCallback(authResult) {
if (authResult['access_token']) {
// Successfully authorized
window.location =
"/MyAccount/GoogleLogOn?returnURL=@(Request["returnURL"])&accessToken="
+ authResult['access_token'];
}
}
Here is my server-side C# code that uses the accessToken provided by Google to obtain the associated Google UserID.
string googleUserID = "";
using (var webClient = new System.Net.WebClient())
{
var json = webClient.DownloadString("https://www.googleapis.com/plus/v1/people/me?access_token=" + accessToken);
JObject o = JObject.Parse(json);
googleUserID = (string)o["id"];
}
Once we have the associated Google UserID, we can poll our database and confirm that a user exists in it with this associated Google UserID. If so, we can log the user in.