Ejemplo n.º 1
0
        public async Task <IActionResult> ForgetPassword(string userEmail, string gRecaptchaResponse)
        {
            try
            {
                //if(!GoogleReCaptchaHelper.IsReCaptchaPassed(gRecaptchaResponse, _googleReCaptchaConfigs.SecretKey, _googleReCaptchaConfigs.URL))
                //{
                //	return BadRequest("CAPTCHA fail");
                //}

                var userName = UserNameHelper.GenerateUserNameFromEmail(userEmail);
                await _emailService.SendForgetPasswordEmailAsync(userName);

                return(NoContent());
            }
            catch (BadRequestException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (NotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"ForgetPassword() Error: {ex}");
                return(StatusCode(500, "Internal Server Error"));
            }
        }
Ejemplo n.º 2
0
        public async Task <LoginUserDto> IssueLoginTokenWithFacebookSignInAsync(FacebookOAuthRequestDto request)
        {
            // 1: verify the token
            var accessTokenVerifyUrl             = string.Format(_facebookOAuthConfigs.TokenVerifyUrl, request.Token, _facebookOAuthConfigs.Access_token);
            var facebookOAuthAccessTokenResponse = await IdentityHelper.CheckFacebookOAuthAccessToken(accessTokenVerifyUrl);

            if (facebookOAuthAccessTokenResponse.Data.User_id != request.UserId)
            {
                throw new BadRequestException("facebook login fail");
            }

            // 2. create user if user not exsist
            var user = await _userRepository.GetUserByUserNameAsync(UserNameHelper.GenerateUserNameFromEmail(request.Email));

            if (user == null)
            {
                user = await ApplicationUser.CreateAsync(_userManager, request.Email, null, request.FirstName, request.LastName, request.ImageUrl);

                user.ManullActivate();
            }
            // 3. reset google login
            await _userManager.RemoveLoginAsync(user, "Facebook", request.UserId);

            await _userManager.AddLoginAsync(user, new UserLoginInfo("Facebook", request.UserId, request.Token));

            // 4. if user data not match with Google
            if (request.FirstName != user.FirstName || request.LastName != user.LastName || request.ImageUrl != user.AvatarUrl)
            {
                user.UpdateProfile(request.FirstName, request.LastName, request.ImageUrl);
            }
            // 5.issue a token to the user
            return(await GenerateLoginUserDto(user));
        }
Ejemplo n.º 3
0
        public async Task <LoginUserDto> IssueLoginTokenWithGoogleSignInAsync(GoogleOAuthRequestDto request)
        {
            // 1: verify the token
            var accessTokenVerifyUrl           = string.Format(_googleOAuthConfigs.AccessTokenVerifyUrl, request.Access_token);
            var googleOAuthAccessTokenResponse = await IdentityHelper.CheckGoogleOAuthAccessToken(accessTokenVerifyUrl);

            // 2. create user if user not exsist
            var user = await _userRepository.GetUserByUserNameAsync(UserNameHelper.GenerateUserNameFromEmail(request.Email));

            if (user == null)
            {
                user = await ApplicationUser.CreateAsync(_userManager, request.Email, null, request.GivenName, request.FamilyName, request.ImageUrl);

                user.ManullActivate();
            }
            // 3. reset google login
            await _userManager.RemoveLoginAsync(user, "Google", request.GoogleId);

            await _userManager.AddLoginAsync(user, new UserLoginInfo("Google", request.GoogleId, request.Id_token));

            // 4. if user data not match with Google
            if (request.GivenName != user.FirstName || request.FamilyName != user.LastName || request.ImageUrl != user.AvatarUrl)
            {
                user.UpdateProfile(request.GivenName, request.FamilyName, request.ImageUrl);
            }
            // 5.issue a token to the user
            return(await GenerateLoginUserDto(user));
        }
Ejemplo n.º 4
0
        public async Task <string> GetUserIdByUserEmail(string email)
        {
            var userName = UserNameHelper.GenerateUserNameFromEmail(email);
            var user     = await _userRepository.GetUserByUserNameAsync(userName);

            if (user == null)
            {
                throw new NotFoundException("User does not exist.");
            }
            return(user.Id);
        }
        public ActionResult Create(MvcKompApp.Models.PersonRemoteValidationModel um)
        {
            if (!UserNameHelper.IsAvailable(um.UserName))
            {
                return(View(um));
            }
            else
            {
                if (!TryUpdateModel(um))
                {
                    return(View(um));
                }

                // ToDo: add persistent to DB.
                UsrLstContainer.getUsrLst().Add(um);
                return(View("Details", um));
            }
        }
Ejemplo n.º 6
0
        public async Task <ApplicationUser> UpdateEmailAsync(UserManager <ApplicationUser> userManager, string newEmail)
        {
            if (Email == newEmail)
            {
                throw new UserUpdateException("The new email should be different");
            }
            EmailConfirmed = false;
            Email          = newEmail;
            UserName       = UserNameHelper.GenerateUserNameFromEmail(newEmail);

            var userResult = await userManager.UpdateAsync(this);

            if (!userResult.Succeeded)
            {
                throw new UserUpdateException(userResult.ToString());
            }

            return(this);
        }
        public string IsUID_Available(string candidate)
        {
            if (UserNameHelper.IsAvailable(candidate))
            {
                return("OK");
            }

            for (int i = 1; i < 10; i++)
            {
                string altCandidate = candidate + i.ToString();
                if (UserNameHelper.IsAvailable(altCandidate))
                {
                    return(String.Format(CultureInfo.InvariantCulture,
                                         "{0} is not available. Try {1}.", candidate, altCandidate));
                }
            }
            return(String.Format(CultureInfo.InvariantCulture,
                                 "{0} is not available.", candidate));
        }
Ejemplo n.º 8
0
        public static async Task <ApplicationUser> CreateAsync(UserManager <ApplicationUser> userManager, string email, string password, string firstName, string lastName, string avatarUrl, IList <string> roles = null)
        {
            // create user
            var user = new ApplicationUser()
            {
                UserName          = UserNameHelper.GenerateUserNameFromEmail(email),
                Email             = email,
                CreateDateUTC     = DateTime.UtcNow,
                LastUpdateDateUTC = DateTime.UtcNow,
                FirstName         = firstName,
                LastName          = lastName,
                AvatarUrl         = avatarUrl,
                // init user's shoppingCart
                ShoppingCart = new ShoppingCart(),
                // init user's Playlists
                Playlists = new List <Playlist>()
                {
                    Playlist.Create(PlaylistsTypeEnum.Favorite, "Favorite"),
                    Playlist.Create(PlaylistsTypeEnum.Favorite, "Viewed")
                }
            };
            // set user password
            IdentityResult userResult = null;

            if (password == null)
            {
                userResult = await userManager.CreateAsync(user);
            }
            else
            {
                userResult = await userManager.CreateAsync(user, password);
            }
            if (!userResult.Succeeded)
            {
                var errorMsg = String.Empty;
                foreach (var error in userResult.Errors)
                {
                    errorMsg += error.Description + " ";
                }
                throw new IdentityException(errorMsg);
            }
            // set user init roles
            if (roles == null)
            {
                var roleResult = await userManager.AddToRoleAsync(user, ApplicationPolicies.DefaultRoles.Student);

                if (!roleResult.Succeeded)
                {
                    throw new IdentityException(roleResult.ToString());
                }
            }
            else
            {
                foreach (var role in roles)
                {
                    var roleResult = await userManager.AddToRoleAsync(user, role);

                    if (!roleResult.Succeeded)
                    {
                        throw new IdentityException(roleResult.ToString());
                    }
                }
            }
            return(user);
        }