Exemplo n.º 1
0
        public async Task <HttpResponseMessage> GetCode(string UserName)
        {
            if (string.IsNullOrEmpty(UserName))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var UserManager = Request.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var user        = await UserManager.FindByNameAsync(UserName);

            if (user == null || string.IsNullOrEmpty(user.Email))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "NoEmail"));
            }
            if (!CRMHelper.IsValidEmail(user.Email))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "InvalidEmail"));
            }

            var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id.ToString());

            if (string.IsNullOrEmpty(code))
            {
                return(Request.CreateResponse(HttpStatusCode.Ambiguous, "NoCode"));
            }

            ForgotPasswordServices _forgotPasswordServices = new ForgotPasswordServices();

            if (!_forgotPasswordServices.SaveTokenRecoveryPassword(code, user.Id))
            {
                return(Request.CreateResponse(HttpStatusCode.Ambiguous, "NoCode"));
            }

            string content = "Please reset your password by code : <b>" + code + "</b>";

            _forgotPasswordServices.SendMailOTPForgotPassword(user, content);
            return(Request.CreateResponse(HttpStatusCode.OK, new
            {
                responseValue =
                    new ResetPasswordResponseModel()
                {
                    UserID = user.Id,
                    //Code = code,
                    //Email = user.Email,
                    ExpiresSeconds = (int)TimeSpan.FromMinutes(15).TotalSeconds
                }
            }));
        }