コード例 #1
0
        /// <summary>
        /// Authorize facebook user in system.
        /// </summary>
        /// <param name="loginFacebookViewModel">The login facebook view model.</param>
        /// <returns></returns>
        public UserInfoViewModel LoginFacebook(LoginFacebookViewModel loginFacebookViewModel)
        {
            User user = this._unitOfWork.UsersRepository.GetFacebookUser(loginFacebookViewModel.Email);
            var  salt = GenerateSalt();

            if (user == null)
            {
                user          = loginFacebookViewModel;
                user.Salt     = salt;
                user.Password = PasswordHashManager.GetPasswordHash(salt, loginFacebookViewModel.Password);
                User addedUser = this._unitOfWork.UsersRepository.Create(user);
                this._unitOfWork.SaveChanges();
                return(this.InitializeUserInfoViewModel(addedUser));
            }
            else
            {
                if (user.FB_Link == null)
                {
                    user.FB_Link = "facebook.com/" + loginFacebookViewModel.FbLink;
                    this._unitOfWork.UsersRepository.Update(user);
                    this._unitOfWork.SaveChanges();
                    return(this.InitializeUserInfoViewModel(user));
                }
            }
            return(this.InitializeUserInfoViewModel(user));
        }
コード例 #2
0
 public string LogInFacebook([FromBody] LoginFacebookViewModel loginFacebookViewModel)
 {
     try
     {
         var userInfo      = this._userDomainService.LoginFacebook(loginFacebookViewModel);
         var userInfoModel = _userDomainService.GetUserInfoViewModel(userInfo.Login);
         return(JsonConvert.SerializeObject(this._getAuthorizationType(userInfoModel),
                                            new JsonSerializerSettings {
             Formatting = Formatting.Indented
         }));
     }
     catch (Exception ex)
     {
         return(this._getAuthorizationTypeError(ex.Message));
     }
 }
コード例 #3
0
        public async Task <JsonResult> Login(LoginFacebookViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                FacebookClient fb   = new FacebookClient(model.FacebookTokenId);
                dynamic        info = fb.Get("me?fields=id");
                //kiem tra user da co hay chua. username chinh la id facebook con password la co san
                var user = await UserManager.FindAsync(info.id, _password);

                //neu da co
                if (user != null)
                {
                    //dang nhap
                    await SignInAsync(user, model.RememberMe);

                    //thanh cong thi tra ve ket qua thanh cong cho client
                    return(Json(new
                    {
                        result = "success",
                        message = GlobalRes.LoginSuccess
                    }));
                }
                else
                {
                    //neu chua co thi tra ve ket qua khong co user ton tai cho client
                    return(Json(new
                    {
                        result = "failed",
                        message = GlobalRes.NoUserExists
                    }));
                }
            }
            // neu id facebook null
            return(Json(new
            {
                result = "failed",
                message = GlobalRes.MessageIdFacebookNull
            }));
        }
コード例 #4
0
        public async Task <IActionResult> LoginFacebook([FromBody] LoginFacebookViewModel model)
        {
            if (!ModelState.IsValid)
            {
                await NotifyErrorModelInvalid();

                return(await ResponseAsync(model));
            }

            //Remover log
            var serializedData = JsonConvert.SerializeObject(model);

            // 1.generate an app access token
            var appAccessTokenResponse = await _httpClient.GetStringAsync($"https://graph.facebook.com/oauth/access_token?client_id={Options.IdAppFacebook}&client_secret={Options.SecretKeyAppFacebook}&grant_type=client_credentials");

            var appAccessToken = JsonConvert.DeserializeObject <ExternalAuthFacebook>(appAccessTokenResponse);

            // 2. validate the user access token
            var userAccessTokenValidationResponse = await _httpClient.GetStringAsync($"https://graph.facebook.com/debug_token?input_token={model.AccessToken}&access_token={appAccessToken.AccessToken}");

            var userAccessTokenValidation = JsonConvert.DeserializeObject <ExternalAuthFacebookTokenValidation>(userAccessTokenValidationResponse);

            //Remover log
            var serializedData2 = JsonConvert.SerializeObject(userAccessTokenValidation);

            if (!userAccessTokenValidation.Data.IsValid)
            {
                await NotifyError("LoginFacebook", "Erro ao efetuar login com Facebook.");

                return(await ResponseAsync(model));
            }

            // 3. we've got a valid token so we can request user data from fb
            //var userInfoResponse = await Client.GetStringAsync($"https://graph.facebook.com/v2.8/me?fields=id,email,first_name,last_name,name,gender,locale,birthday,picture&access_token={model.AccessToken}");
            //var userInfo = JsonConvert.DeserializeObject<FacebookUserData>(userInfoResponse);

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                user = new ApplicationUser {
                    Nome = model.Nome, UserName = model.Email, Email = model.Email, FacebookId = model.FacebookId, PictureUrl = model.PictureUrl
                };

                var resultCreateUser = await _userManager.CreateAsync(user, "X0a@" + Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Substring(0, 8));

                if (!resultCreateUser.Succeeded)
                {
                    await NotifyError("LoginFacebook", "Erro ao efetuar login com Facebook..");

                    await _userManager.DeleteAsync(user);

                    return(await ResponseAsync(model));
                }
            }

            user = await _userManager.FindByEmailAsync(model.Email);

            if (!await _userManager.IsEmailConfirmedAsync(user))
            {
                var resultConfirmEmail = await _userManager.ConfirmEmailAsync(user, await _userManager.GenerateEmailConfirmationTokenAsync(user));

                if (resultConfirmEmail.Succeeded)
                {
                    LoginViewModel loginViewModel = new LoginViewModel();
                    loginViewModel.Email = model.Email;
                    var response = await GenerateToken(loginViewModel);

                    return(await ResponseAsync(response));
                }
                else
                {
                    await NotifyError("LoginFacebook", "Erro ao efetuar login com Facebook...");

                    await _userManager.DeleteAsync(user);

                    return(await ResponseAsync(model));
                }
            }
            else
            {
                LoginViewModel loginViewModel = new LoginViewModel();
                loginViewModel.Email = model.Email;
                var response = await GenerateToken(loginViewModel);

                return(await ResponseAsync(response));
            }
        }