Пример #1
0
        public async Task <ActionResult> ResetAcessTokenPassword(ResetTokenPasswordModel model, [FromServices] IMemoryCache cache)
        {
            string jsonError = ErrorFormat.SerializeError(new BadRequestError("Invalid reset credentiais"));
            var    user      = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(BadRequest(jsonError));
            }

            var lockoutEndDate = await _userManager.GetLockoutEndDateAsync(user);

            if (lockoutEndDate > DateTimeOffset.Now)
            {
                return(BadRequest(ErrorFormat.SerializeError(new BadRequestError("Too many attempts to reset. Wait a few minutes and try again"))));
            }

            var token = cache.Get(model.Code) as string;

            if (token == null)
            {
                var time = DateTimeOffset.Now.AddSeconds(30);
                await _userManager.SetLockoutEndDateAsync(user, time);

                var error = new BadRequestError();
                return(BadRequest(jsonError));
            }
            //await _userManager.ResetPasswordAsync(user, token, model.Password);
            return(Ok(new ResetAcessTokenPasswordModel(token)));
        }
Пример #2
0
        public async Task <ActionResult> Register(CustomRegisterUser registerUser)
        {
            var user = new MyIdentityUser
            {
                UserName       = registerUser.Name,
                Email          = registerUser.Email,
                EmailConfirmed = true
            };

            var result = await _userManager.CreateAsync(user, registerUser.Password);


            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, Roles.BASIC);

                await _userManager.AddClaimAsync(user, new Claim(Claims.Sample, ClaimsValues.READ));

                await _userManager.AddClaimAsync(user, new Claim(Claims.Sample, ClaimsValues.WRITE));

                return(Ok(createToken(user.UserName)));
            }
            else
            {
                var error = new BadRequestError();
                foreach (var item in result.Errors)
                {
                    error.AddMessage(item.Description);
                }
                var json = ErrorFormat.SerializeError(ModelState, error);
                return(BadRequest(json));
            }
        }
Пример #3
0
        public async Task <ActionResult> ForgotPassword(ForgotPasswordModel model, [FromServices] IMemoryCache cache)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                string jsonError = ErrorFormat.SerializeError(new BadRequestError("The email address does not exist."));
                return(BadRequest(jsonError));
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(user);

            var code = RandomGenerator.GenerateString(8);

            cache.Set(code, token);

            await _emailService.SendEmailAsync(model.Email, "Reset password account.", "Code:" + code);

            return(Ok());
        }
Пример #4
0
        public async Task <ActionResult> Login(CustomLoginUser loginUser)
        {
            var result = await _signInManager.PasswordSignInAsync(loginUser.Name, loginUser.Password, false, true);

            if (result.Succeeded)
            {
                var tokeResponse = createToken(loginUser.Name);
                return(Ok(tokeResponse));
            }



            if (result.IsLockedOut)
            {
                string jsonError = ErrorFormat.SerializeError(new BadRequestError("Incorrect user or password"));
                return(BadRequest(jsonError));
            }

            string json = ErrorFormat.SerializeError(new BadRequestError("Incorrect user or password"));

            return(BadRequest(json));
        }
Пример #5
0
        public async Task <ActionResult> ResetPassword(ResetPasswordModel model, [FromServices] IMemoryCache cache)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(BadRequest(ErrorFormat.SerializeError(new BadRequestError("Invalid reset credentiais"))));
            }

            var result = await _userManager.ResetPasswordAsync(user, model.Token, model.Password);

            if (!result.Succeeded)
            {
                var error = new BadRequestError();
                foreach (var item in result.Errors)
                {
                    error.AddMessage(item.Description);
                }
                var json = ErrorFormat.SerializeError(ModelState, error);
                return(BadRequest(json));
            }

            return(Ok("Ok"));
        }