public ApiResult <ForgetPasswordViewModel> ForgetPassword(ForgetPasswordViewModel model) { var dbResult = new ApiResult <ForgetPasswordViewModel>(); string errorMessage = string.Empty; try { // Kiểm tra dữ liệu var ValidPassword = ValidateForgetPassword(model); if (!ValidPassword.Succeeded) { return(ValidPassword); } // Tạo token code và thời gian hết hạn string tokenCode = EncryptCore.Md5Get(DateTime.Now.Ticks + model.Email + EncryptCore.KeyEncrypt);; DateTime tokenExp = DateTime.Now.AddDays(1); // Lấy kết quả trả về từ DB dbResult = userDAL.ForgetPassword(model, tokenCode, tokenExp); // Kiểm tra kết quả if (dbResult.Succeeded) { // Tạo nội dung của email var otpEmailFormat = new { HoTen = dbResult.Data.FirstName, Link = model.UrlForm + "?Token=" + tokenCode }; var msgEmailOtp = EmailContentHtml.EmailContentFormat(otpEmailFormat, "ResetPassword.html"); // Gửi email cho user var emailSendStt = EmailUtility.SendMail(ConfigUtil.Email_DisplayName, model.Email, "Quên mật khẩu", msgEmailOtp); } return(dbResult); } catch (Exception ex) { dbResult.Failed(new ErrorObject() { Code = Constants.ERR_EXCEPTION, Description = ex.Message }); return(dbResult); } }
public IHttpActionResult Login(UserPostLogin item) { var Rs = new ApiResult <UserResult>(); try { if (string.IsNullOrEmpty(item.Email)) { Rs.Failed(new ErrorObject() { Code = "1", Description = "Chưa nhập Email" }); } if (string.IsNullOrEmpty(item.Password)) { Rs.Failed(new ErrorObject() { Code = "2", Description = "Chưa nhập Password" }); } if (Rs.Errors.Count > 0) { return(Content(HttpStatusCode.Unauthorized, Rs)); } var Login = userDAL.Login(item); if (Login.Succeeded && Login.Data != null) { if (Libs.GetMd5(item.Password + EncryptCore.PassKey) != Login.Data.Password) { Rs.Failed(new ErrorObject { Code = "400", Description = "Sai Mật Khẩu" }); return(Content(HttpStatusCode.Unauthorized, Rs)); } Random rnd = new Random(); int RndNumber = rnd.Next(1, 9999); var TokenCode = ""; var User = Login.Data; if (User.EmailConfirmed > 0) { TokenCode = EncryptCore.Md5Get(item.Email + User.Id + EncryptCore.PassKey + DateTime.Now.ToString("ddMMyyyyhhmmss") + RndNumber); } else { Rs.Failed(new ErrorObject { Code = "EMAIL_NOT_CONFIRM", Description = "Email is not comfirmed" }); } var UserInf = new UserResult() { Id = User.Id, Email = User.Email, AccessToken = TokenCode, FirstName = User.FirstName, LastName = User.LastName, PhoneNumber = User.PhoneNumber, Address = User.Address, DistrictCode = User.DistrictCode, ProvinceCode = User.ProvinceCode, RoleDesc = User.RoleDesc, RoleId = User.RoleId, UserAvatar = User.UserAvatar, EmailConfirmed = User.EmailConfirmed }; CacheUtil.InsertCacheObject(TokenCode, UserInf, 60 * 24 * 30); Rs.Data = UserInf; return(Content(HttpStatusCode.OK, Rs)); } else { Rs.Failed(new ErrorObject { Code = Login.Errors[0].Code, Description = Login.Errors[0].Description }); return(Content(HttpStatusCode.BadRequest, Rs)); } } catch (Exception ex) { Rs.Failed(new ErrorObject() { Code = Constants.ERR_EXCEPTION, Description = ex.ToString() }); return(Content(HttpStatusCode.Unauthorized, Rs)); } }