OpenId Summary2 (Google Account)

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");
        }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值