Exemplo n.º 1
0
        /// <summary>
        /// Xác thực tài khoản thông qua việc login bằng các tài khoản FB hoặc GG.
        /// Author       :   QuyPN - 30/05/2018 - create
        /// </summary>
        /// <param name="accessToken">Thông tin cá nhân lấy được từ FB hoặc GG</param>
        /// <param name="type">type = 1: thông tin từ FB; type = 2: thông tin từ GG</param>
        /// <returns>Chỗi Json chứa kết quả kiểm tra</returns>
        /// <remarks>
        /// Method: GET
        /// RouterName: homeCheckSocialLogin
        /// </remarks>
        public JsonResult LoginBySocialAccount(string accessToken, int type = (int)OtherEnum.TaiKhoanFB)
        {
            ResponseInfo response = new ResponseInfo();

            try
            {
                LoginModel    model = new LoginModel();
                TblTokenLogin token = model.CheckSocialAccount(type == (int)OtherEnum.TaiKhoanFB
                    ? model.LayThongTinFB(accessToken)
                    : model.LayThongTinGG(accessToken), type);
                if (token != null)
                {
                    response.ThongTinBoSung1 = BaoMat.Base64Encode(token.Token);
                }
                else
                {
                    response.Code  = (int)CodeResponse.NotAccess;
                    response.MsgNo = (int)MsgNO.XacThucKhongHopLe;
                }
            }
            catch (Exception e)
            {
                response.Code            = (int)CodeResponse.ServerError;
                response.MsgNo           = (int)MsgNO.ServerError;
                response.ThongTinBoSung1 = e.Message;
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 2
0
        /// <summary>
        /// So khớp thông tin đăng nhập có thừ FB hoặc GG với thông tin tài khoản của hệ thống.
        /// Author       :   QuyPN - 30/05/2018 - create
        /// </summary>
        /// <param name="socialAccount">Thông tin cá nhân lấy được từ FB hoặc GG</param>
        /// <param name="type">type = 1: thông tin từ FB; type = 2: thông tin từ GG</param>
        /// <returns>Đối tượng chứ token login của tài khoản trong hệ thống</returns>
        public TblTokenLogin CheckSocialAccount(SocialAccount socialAccount, int type = (int)OtherEnum.TaiKhoanFB)
        {
            DbContextTransaction transaction = context.Database.BeginTransaction();

            try
            {
                if (socialAccount.Id != "")
                {
                    TblAccount account = context.Account.FirstOrDefault(x => x.Email == socialAccount.Email && !x.DelFlag);
                    if (account == null)
                    {
                        account = context.Account.FirstOrDefault(x => (
                                                                     (type == (int)OtherEnum.TaiKhoanFB && x.IdFacebook == socialAccount.Id) ||
                                                                     (type != (int)OtherEnum.TaiKhoanFB && x.IdGoogle == socialAccount.Id)) && !x.DelFlag);
                        if (account != null && socialAccount.Email != "")
                        {
                            account.Email = socialAccount.Email;
                        }
                    }
                    else
                    {
                        if (type == (int)OtherEnum.TaiKhoanFB)
                        {
                            account.IdFacebook = socialAccount.Id;
                        }
                        else
                        {
                            account.IdGoogle = socialAccount.Id;
                        }
                    }
                    if (account == null)
                    {
                        TblUser user = new TblUser
                        {
                            Ho          = socialAccount.FirstName,
                            Ten         = socialAccount.LastName,
                            Avatar      = "http://2.bp.blogspot.com/-Fl8NZJZFq6w/U02LSHQ7iII/AAAAAAAAAHg/zpzikQfynpM/s1600/WAPHAYVL.MOBI-CONDAU+(11).gif",
                            GioiTinh    = socialAccount.Gender,
                            NgaySinh    = socialAccount.Birthday,
                            SoDienThoai = socialAccount.PhoneNumber,
                            CMND        = "",
                            DiaChi      = ""
                        };
                        account = new TblAccount
                        {
                            Username         = "",
                            Password         = "",
                            Email            = socialAccount.Email,
                            TokenActive      = "",
                            IsActived        = true,
                            IsActiveEmail    = true,
                            SoLanDangNhapSai = 0,
                            KhoaTaiKhoanDen  = DateTime.Now
                        };
                        if (type == (int)OtherEnum.TaiKhoanFB)
                        {
                            account.IdFacebook = socialAccount.Id;
                        }
                        else
                        {
                            account.IdGoogle = socialAccount.Id;
                        }
                        account.GroupOfAccount.Add(new TblGroupOfAccount
                        {
                            IdGroup = (int)GroupAccount.User
                        });
                        user.Account.Add(account);
                        context.User.Add(user);
                    }
                    account.SoLanDangNhapSai = 0;
                    account.IsActived        = true;
                    TblCauHinh    cauHinh    = context.CauHinh.FirstOrDefault(x => x.Id == (int)OtherEnum.IdCauHinh);
                    TblTokenLogin tokenLogin = new TblTokenLogin
                    {
                        Token          = Common.GetToken(account.Id),
                        ThoiGianTonTai = DateTime.Now.AddHours(cauHinh.ThoiGianTonTaiToken)
                    };
                    account.TokenLogin.Add(tokenLogin);
                    context.SaveChanges();
                    transaction.Commit();
                    return(tokenLogin);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception e)
            {
                transaction.Rollback();
                throw e;
            }
        }