示例#1
0
        /// <summary>
        /// LoginWithGooglePlus
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public JsonResult LoginWithGooglePlus(string accessToken)
        {
            try
            {
                var clientId = ConfigurationManager.AppSettings["GooglePlusClientId"];

                var jsonResult = this.GetWebResponse("https://people.googleapis.com/v1/people/me?personFields=metadata,names,emailAddresses&access_token=" + accessToken, HttpMethod.Get, null);

                if (!String.IsNullOrEmpty(jsonResult))
                {
                    var response = JValue.Parse(jsonResult);
                    var error    = response.Value <JObject>("error");

                    if (error != null)
                    {
                        Console.WriteLine(error.Value <string>("message"));
                        return(Json(new ReturnObj
                        {
                            StatusCode = (int)RegisterStatus.第三方登入失敗,
                            Message = RegisterStatus.第三方登入失敗.ToString(),
                        }));
                    }
                    else
                    {
                        GoogleAuthModel AuthModel = JsonConvert.DeserializeObject <GoogleAuthModel>(jsonResult);//反序列化

                        var userId = AuthModel.resourceName.Replace("people/", "");

                        Name   name_modle = AuthModel.names.Find(x => x.metadata.primary == true);
                        string userName   = null;
                        if (name_modle != null)
                        {
                            userName = name_modle.displayName;
                        }

                        GoogleAuthEmailAddresses emailAddresses_model = AuthModel.emailAddresses.Find(x => x.metadata.primary == true);
                        string userEmail = null;
                        if (emailAddresses_model != null)
                        {
                            userEmail = emailAddresses_model.value;
                        }

                        // 確認第三方登入的會員有沒有註冊過 沒有的話就讓使用者填寫註冊資料

                        if (string.IsNullOrWhiteSpace(userEmail))
                        {
                            Console.WriteLine("Google Plus Email is Null");


                            return(Json(new ReturnObj
                            {
                                StatusCode = (int)RegisterStatus.第三方授權無法提供您的EMail,
                                Message = "第三方授權無法提供您的E - Mail,請您改用其他方式註冊(登入)",
                            }));
                        }

                        var data = service.HasExtauth(userEmail, "GL", userId);

                        if (data.Status == RegisterStatus.第三方登入失敗)
                        {
                            return(Json(new ReturnObj
                            {
                                StatusCode = (int)data.Status,
                                Message = data.Status.ToString(),
                            }));
                        }

                        if (data.Status == RegisterStatus.註冊直接登入)
                        {
                            var verifyForSocialData = new VerifyForSocialDto()
                            {
                                Account    = userEmail,
                                Pwd        = "#",
                                Ip         = Request.UserHostAddress,
                                Browser    = Request.Browser.Browser,
                                ProvdrType = "GL",
                                UserId     = userId
                            };

                            var verifyStatus = service.VerifyForSocial(verifyForSocialData);

                            // Login
                            if (!verifyStatus)
                            {
                                return(Json(new ReturnObj
                                {
                                    StatusCode = (int)RegisterStatus.第三方登入失敗,
                                    Message = RegisterStatus.第三方登入失敗.ToString(),
                                }));
                            }
                            else
                            {
                                return(Json(new ReturnObj
                                {
                                    StatusCode = (int)data.Status,
                                    Message = data.Status.ToString(),
                                }));
                            }
                        }
                        else
                        {
                            data.UserId     = userId;
                            data.Email      = userEmail;
                            data.ProvdrType = "GL";

                            var result = new ReturnObj
                            {
                                StatusCode = (int)data.Status,
                                Message    = data.Status.ToString(),
                                Email      = userEmail,
                                UserID     = userId,
                                UserName   = userName,
                                Phone      = data.Phone
                            };

                            //sessionRegistrationInfo = result;
                            service.TempRegistrationInfo(new RegisterDto()
                            {
                                Account         = userEmail,
                                Pwd             = "#",
                                UserId          = userId,
                                ProvdrType      = "GL",
                                Email           = userEmail,
                                ClientIP        = Request.UserHostAddress,
                                HasNotifyLetter = "N",
                                CustId          = "",
                                RecommendId     = "",
                                Mobile          = "",
                            },
                                                         new SorceOptionDto()
                            {
                                otherSorce = "",
                                SorceValue = ""
                            });

                            return(Json(result));
                        }
                    }
                }
                else
                {
                    return(Json(new ReturnObj
                    {
                        StatusCode = (int)RegisterStatus.第三方登入失敗,
                        Message = RegisterStatus.第三方登入失敗.ToString(),
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new ReturnObj
                {
                    StatusCode = (int)RegisterStatus.第三方登入失敗,
                    Message = RegisterStatus.第三方登入失敗.ToString(),
                }));
            }
        }
示例#2
0
        /// <summary>
        /// LoginWithFacebook
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>

        public JsonResult LoginWithFacebook(string accessToken)
        {
            try
            {
                var jsonResult = this.GetWebResponse("https://graph.facebook.com/v3.0/me?access_token=" + accessToken + "&fields=id%2Cname%2Cemail%2Cgender%2Cbirthday%2Cpicture&format=json&method=get&pretty=0&suppress_http_code=1", HttpMethod.Get);
                if (!String.IsNullOrEmpty(jsonResult))
                {
                    var response = JValue.Parse(jsonResult);
                    var error    = response.Value <JObject>("error");

                    if (error != null)
                    {
                        Console.WriteLine(error);
                        return(Json(new ReturnObj
                        {
                            StatusCode = (int)RegisterStatus.第三方登入失敗,
                            Message = RegisterStatus.第三方登入失敗.ToString(),
                        }));
                    }
                    else
                    {
                        var userId    = response.Value <string>("id");
                        var userName  = response.Value <string>("name");
                        var userEmail = response.Value <string>("email");

                        if (string.IsNullOrWhiteSpace(userEmail))
                        {
                            Console.WriteLine("FB Email is Null");
                            return(Json(new ReturnObj
                            {
                                StatusCode = (int)RegisterStatus.第三方授權無法提供您的EMail,
                                Message = "第三方授權無法提供您的E - Mail,請您改用其他方式註冊(登入)",
                            }));
                        }

                        var data = service.HasExtauth(userEmail, "FB", userId);

                        if (data.Status == RegisterStatus.第三方登入失敗)
                        {
                            return(Json(new ReturnObj
                            {
                                StatusCode = (int)data.Status,
                                Message = data.Status.ToString(),
                            }));
                        }

                        if (data.Status == RegisterStatus.註冊直接登入)
                        {
                            var verifyForSocialData = new VerifyForSocialDto()
                            {
                                Account    = userEmail,
                                Pwd        = "#",
                                Ip         = Request.UserHostAddress,
                                Browser    = Request.Browser.Browser,
                                ProvdrType = "FB",
                                UserId     = userId
                            };

                            var verifyStatus = service.VerifyForSocial(verifyForSocialData);

                            // Login
                            if (!verifyStatus)
                            {
                                return(Json(new ReturnObj
                                {
                                    StatusCode = (int)RegisterStatus.第三方登入失敗,
                                    Message = RegisterStatus.第三方登入失敗.ToString(),
                                }));
                            }
                            else
                            {
                                return(Json(new ReturnObj
                                {
                                    StatusCode = (int)data.Status,
                                    Message = data.Status.ToString(),
                                }));
                            }
                        }
                        else
                        {
                            data.UserId     = userId;
                            data.Email      = userEmail;
                            data.ProvdrType = "FB";

                            var result = new ReturnObj
                            {
                                StatusCode = (int)data.Status,
                                Message    = data.Status.ToString(),
                                Email      = userEmail,
                                UserID     = userId,
                                UserName   = userName,
                                Phone      = data.Phone
                            };

                            //sessionRegistrationInfo = result;
                            service.TempRegistrationInfo(new RegisterDto()
                            {
                                Account         = userEmail,
                                Pwd             = "#",
                                UserId          = userId,
                                ProvdrType      = "FB",
                                Email           = userEmail,
                                ClientIP        = Request.UserHostAddress,
                                HasNotifyLetter = "N",
                                CustId          = "",
                                RecommendId     = "",
                                Mobile          = "",
                            },
                                                         new SorceOptionDto()
                            {
                                otherSorce = "",
                                SorceValue = ""
                            });

                            return(Json(result));
                        }
                    }
                }
                else
                {
                    return(Json(new ReturnObj
                    {
                        StatusCode = (int)RegisterStatus.第三方登入失敗,
                        Message = RegisterStatus.第三方登入失敗.ToString(),
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new ReturnObj
                {
                    StatusCode = (int)RegisterStatus.第三方登入失敗,
                    Message = RegisterStatus.第三方登入失敗.ToString(),
                }));
            }
        }