1,Google Article for login with google
http://code.google.com/apis/accounts/docs/OAuth2Login.html
2, Register your web app into Google
https://code.google.com/apis/console/b/0/?pli=1#project:462369903149:access
3,Login Button
<a href="https://accounts.google.com/o/oauth2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&
state=%2Fprofile&
redirect_uri=http://m.4pmdesign.com/Account/googleOauth&
response_type=token&
client_id=462369903149.apps.googleusercontent.com"><img src="../../Content/googleicon.jpg" /> </a>
4, When Login success, it will redirect to redirect_uri page, with some parameters
http://m.4pmdesign.com/Account/googleOauth#state=/profile&access_token=ya29.AHES6ZQo4AvsOW8O7QTPtom9AI7AuyigUjqG4G1w-ByLgCuXOMQ3XMU&token_type=Bearer&expires_in=3600
5, it need use javascript to get the value after hash(#), and sent it to action for next step
<script>
window.onload = function testSp() {
document.getElementById('access_token').value = location.hash; // get the url parameters after hash #
}
</script>
<img src="../../Content/4pmdesign-logo_beta.png" />
<div id="click" style="cursor: pointer;"><h1>Click Here to Continue</h1></div>
<script type="text/javascript"> // handle when click, send value to action
$("#click").click(function () {
var query = "/Account/googleOauthLogin?";
query += "access_token=";
query += $("#google").serialize();
window.location = query;
});
</script>
<form id="google">
<input id="access_token" name="access_token" type="hidden" /> // hidden input field
</form>
6,handle the url, and use web request get the json result, then use json.net(need install) to read it, then is same as facebook one.
//GET: /Account/googleOauthLogin
public ActionResult googleOauthLogin(string access_token)
{
int index1 = access_token.IndexOf("ya29");
int index2 = access_token.IndexOf("&token_type");
string at = access_token.Substring(index1, index2 - index1); // this is for get paramater -- access_token -- from # redirection url
string res_url = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + at;
WebRequest request = WebRequest.Create(res_url); // use web request for get json array return
request.ContentType = "application/json; charset=utf-8";
string text;
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream())) //read and save response as string
{
text = sr.ReadToEnd();
}
string json = JsonConvert.SerializeObject(text);
PersonInputModel pim = JsonConvert.DeserializeObject<PersonInputModel>(text); // convert & read json result, save in the model -- PersonInputModel
if (Membership.ValidateUser(pim.email, pim.id))
{
FormsAuthentication.SetAuthCookie(pim.email, true);
return RedirectToAction("Index", "Home");
}
else
{
MembershipCreateStatus createStatus;
Membership.CreateUser(pim.name, pim.id, pim.email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(pim.email, true);
return RedirectToAction("Index", "Home");
}
}
return RedirectToAction("Index", "Home");
}