public async Task <ActionResult <ProfilersDTO> > Profilers(string UserId) { var response = new ProfilersDTO(); try { var profile = new AspNetUserProfiles(); profile = _repositoryWrapper.AspNetUserProfiles.FirstOrDefault(p => p.UserId == UserId); if (profile == null) { _logger.LogError($"[ManageController] {ConstMessage.GetMsgConst("ACC008")}"); response.ErrorCode = "ACC008"; response.Message = ConstMessage.GetMsgConst("ACC008"); return(response); } var result = _mapper.Map <ProfilersDTO>(profile); return(result); } catch (Exception exception) { _logger.LogError($"[ManageController] {exception.ToString()}"); response.Message = $"Có lỗi trong quá trình cập nhật"; return(response); } }
public async Task <object> ResendCode([FromBody] ForgotPassworDTO model) { var responseModel = new ModelBase(); var code = ""; var user = await _userManager.FindByNameAsync(model.EmailOrPhone); if (user == null) { responseModel.ErrorCode = "ACC008"; responseModel.Message = ConstMessage.GetMsgConst("ACC008"); return(responseModel); } if (!Util.IsPhoneNumber(model.EmailOrPhone)) { code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.EmailOrPhone); } else { code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.EmailOrPhone); } //await SendCode(Util.IsPhoneNumber(model.EmailOrPhone) ? "Phone" : "Email", model.EmailOrPhone, code); responseModel.ErrorCode = "00"; responseModel.Message = "Đã gửi code xác nhận"; return(responseModel); }
private async Task <LoginSocialDto> LoginSocialReturn(string userIDSocial, LoginSocialDto model) { var responseModel = new LoginSocialDto(); responseModel.Email = model.Email; responseModel.LoginProvider = model.LoginProvider; responseModel.ProviderKey = model.ProviderKey; if (!String.IsNullOrEmpty(userIDSocial)) { var appUser = _userManager.Users.SingleOrDefault(r => r.Id == userIDSocial); responseModel.Email = model.Email; var lstRole = await _userManager.GetRolesAsync(appUser); //responseModel.Role = lstRole.Count > 0 ? string.Join(",", lstRole) : ""; var jwt = await GenerateJwtToken(model.Email, appUser); responseModel.JWT = jwt.ToString(); responseModel.Id = appUser.Id; return(responseModel); } else { responseModel.ErrorCode = "005"; responseModel.Message = ConstMessage.GetMsgConst("005"); return(responseModel); } }
public async Task <object> SetPassword([FromBody] SetPassDTO model) { var responseModel = new SetPassDTO(); responseModel.EmailOrPhone = model.EmailOrPhone; var user = await _userManager.FindByNameAsync(model.EmailOrPhone); if (user == null) { responseModel.ErrorCode = "ACC008"; responseModel.Message = ConstMessage.GetMsgConst("ACC008"); return(responseModel); } user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, model.NewPassword); var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { responseModel.ErrorCode = "00"; responseModel.Message = "Cập nhật mật khẩu thành công"; return(responseModel); } else { responseModel.ErrorCode = "002"; responseModel.Message = ConstMessage.GetMsgConst("002"); return(responseModel); } }
public async Task <object> ChangePassword([FromBody] ChangePassDTO model) { var responseModel = new ChangePassDTO(); responseModel.Email = model.Email; var InputEmail = model.Email; if (Util.IsPhoneNumber(model.Email)) { InputEmail = $"{model.Email}@hanoma.vn"; } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Check input Email Pass if (String.IsNullOrEmpty(model.Email) || String.IsNullOrEmpty(model.NewPassword) || String.IsNullOrEmpty(model.OldPassword)) { responseModel.ErrorCode = "ACC006"; responseModel.Message = ConstMessage.GetMsgConst("ACC006"); return(responseModel); } //Check exists User //if (_repositoryWrapper.AspNetUsers.CheckExistsUser(InputEmail)) //{ // responseModel.ErrorCode = "ACC009"; // responseModel.Message = ConstMessage.GetMsgConst("ACC009"); // return responseModel; //} var user = await _userManager.FindByNameAsync(model.Email); if (user == null) { responseModel.ErrorCode = "ACC008"; responseModel.Message = ConstMessage.GetMsgConst("ACC008"); return(responseModel); } var changePasswordResult = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword); if (changePasswordResult.Succeeded) { responseModel.ErrorCode = "00"; responseModel.Message = "Thiết lập mật khẩu thành công"; return(Ok(responseModel)); } else { responseModel.ErrorCode = "ACC015"; responseModel.Message = ConstMessage.GetMsgConst("ACC015"); return(responseModel); } }
public async Task <object> ForgotPassword([FromBody] ForgotPassworDTO model) { var responseModel = new ForgotPassworDTO(); responseModel.EmailOrPhone = model.EmailOrPhone; if (Util.IsPhoneNumber(model.EmailOrPhone)) { //var modelPhone = $"{model.EmailOrPhone}@hanoma.vn"; var user = await _userManager.FindByNameAsync(model.EmailOrPhone); if (user == null) { responseModel.ErrorCode = "ACC008"; responseModel.Message = ConstMessage.GetMsgConst("ACC008"); return(responseModel); } var code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.EmailOrPhone); await SendCode(Util.IsPhoneNumber(model.EmailOrPhone)? "Phone" : "Email", model.EmailOrPhone, code); responseModel.ErrorCode = "00"; responseModel.Message = "Đã gửi code xác nhận"; return(responseModel); } else { var user = await _userManager.FindByNameAsync(model.EmailOrPhone); if (user == null) { responseModel.ErrorCode = "ACC008"; responseModel.Message = ConstMessage.GetMsgConst("ACC008"); return(responseModel); } var codeMail = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.EmailOrPhone); await SendCode(Util.IsPhoneNumber(model.EmailOrPhone)? "Phone" : "Email", model.EmailOrPhone, codeMail); //await _emailSender.SendEmailAsync(model.EmailOrPhone, "Mã xác thực lấy lại mật khẩu", $"Mã xác thực của bạn là:{codeMail}",_repositoryWrapper.AspNetUsers.setting()); //Util.SendMail("",model.EmailOrPhone,"","Xác thực lấy lại mật khẩu",$"Mã xác thực của bạn là:{codeMail}",_repositoryWrapper.AspNetUsers.setting()); responseModel.ErrorCode = "00"; responseModel.Message = "Đã gửi code xác nhận qua email"; return(responseModel); } }
public async Task <object> VerifyCode(string EmailOrPhone, string code) { var responseModel = new ConfirmVerify(); responseModel.EmailOrPhone = EmailOrPhone; responseModel.Code = code; var phoneNumEmail = EmailOrPhone; if (Util.IsPhoneNumber(EmailOrPhone)) { phoneNumEmail = $"{EmailOrPhone}@hanoma.vn"; } var user = await _userManager.FindByNameAsync(EmailOrPhone); if (user == null) { responseModel.ErrorCode = "ACC008"; responseModel.Message = ConstMessage.GetMsgConst("ACC008"); return(responseModel); } if (code == "123456") { //Upgrade Mail Confirm var codeMail = await _userManager.GenerateEmailConfirmationTokenAsync(user); var result2 = await _userManager.ConfirmEmailAsync(user, codeMail); var codePhone = await _userManager.GenerateChangePhoneNumberTokenAsync(user, ""); var result3 = await _userManager.ChangePhoneNumberAsync(user, "", codePhone); responseModel.ErrorCode = "00"; responseModel.Message = "Verify Thành công"; return(responseModel); } else { responseModel.ErrorCode = "ACC012"; responseModel.Message = "Verify không thành công"; return(responseModel); } }
public async Task <ActionResult <ProfilersDTO> > UpdateProfilers(string userId, ProfilersDTO model) { try { if (String.IsNullOrEmpty(userId) || String.IsNullOrEmpty(model.UserId)) { model.ErrorCode = "ACC008"; model.Message = ConstMessage.GetMsgConst("ACC008") + "userId=" + userId + "Model Userid =" + model.UserId; return(model); } var profile = _repositoryWrapper.AspNetUserProfiles.FirstOrDefault(p => p.UserId == userId); if (profile == null) { _logger.LogError($"[ManageController] {ConstMessage.GetMsgConst("ACC008")}"); model.ErrorCode = "ACC008"; model.Message = ConstMessage.GetMsgConst("ACC008"); return(model); } else { model.UserId = userId; _mapper.Map(model, profile); _repositoryWrapper.AspNetUserProfiles.UpdateProfilers(profile); _repositoryWrapper.Save(); } var result = _mapper.Map <ProfilersDTO>(profile); result.ErrorCode = "00"; result.Message = "Cập nhật thành công"; return(result); } catch (Exception exception) { _logger.LogError($"[ManageController] {exception.ToString()}"); model.ErrorCode = "01"; model.Message = $"Có lỗi trong quá trình cập nhật"; return(model); } }
public async Task <UpdateAvatarDTO> UpdateAvatar(ImageUploadAvatarDTO model) { model.ExtensionType = "image/jpeg"; var response = new UpdateAvatarDTO(); var profile = new AspNetUserProfiles(); profile = _repositoryWrapper.AspNetUserProfiles.FirstOrDefault(p => p.UserId == model.UserId); if (profile == null) { _logger.LogError($"[ManageController] {ConstMessage.GetMsgConst("ACC008")}"); response.ErrorCode = "ACC008"; response.Message = ConstMessage.GetMsgConst("ACC008"); return(response); } try { if (model.Base64.Length > 0) { using (var client = new HttpClient()) { //client.BaseAddress = new Uri("https://cdn.hanoma.vn/api/UploadFile/UploadSingleImage"); //client.DefaultRequestHeaders.Accept.Clear(); //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); model.PathSave = "user/avatar/original"; var stringContent = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"); //var buffer = System.Text.Encoding.UTF8.GetBytes(content); //var byteContent = new ByteArrayContent(buffer); // HTTP POST //HttpResponseMessage responseSent = await client.PostAsync("https://cdn.hanoma.vn/api/UploadFile/UploadSingleImage", stringContent); //File Extension Type model.FileName = profile.UserId + "-" + DateTime.Now.ToString("dd-MM-yyyy") + "-" + DateTime.Now.ToString("HH-mm-ss") + "." + model.ExtensionType.Replace("image/", ""); var responseUpload = await UploadImage(model); if (responseUpload) { response.UserId = model.UserId; response.AvatarUrl = model.FileName; _mapper.Map(response, profile); _repositoryWrapper.AspNetUserProfiles.UpdateProfilers(profile); _repositoryWrapper.Save(); response.ErrorCode = "00"; response.Message = "Upload thành công"; return(response); } else { response.ErrorCode = "002"; response.Message = ConstMessage.GetMsgConst("002"); return(response); } } } else { response.ErrorCode = "ACC014"; response.Message = ConstMessage.GetMsgConst("ACC014"); return(response); } } catch (Exception ex) { response.ErrorCode = "002"; response.Message = ConstMessage.GetMsgConst("002") + " " + ex.Message.ToString(); return(response); } }
public async Task <PostProductBrandDTO> PostProductBrand(PostProductBrandDTO model) { _logger.LogDebug($"PostProductBrand: {JsonConvert.SerializeObject(model)}"); var output = new PostProductBrandDTO(); if (!Util.IsEmail(model.Data.Email)) { output.ErrorCode = "BRAND001"; output.Message = Utils.ConstMessage.GetMsgConst("BRAND001"); return(output); } if (!Util.IsPhoneNumber(model.Data.Mobile?.Trim())) { output.ErrorCode = "BRAND002"; output.Message = Utils.ConstMessage.GetMsgConst("BRAND002"); return(output); } if (String.IsNullOrEmpty(model.Data.Address)) { output.ErrorCode = "BRAND005"; output.Message = Utils.ConstMessage.GetMsgConst("BRAND005"); return(output); } var UserProfilers = _repoWrapper.AspNetUserProfiles.FirstOrDefault(p => p.UserId == model.UserId); if (UserProfilers != null) { if (UserProfilers.ProductBrand_ID != null & UserProfilers.ProductBrand_ID > 0) { output.ErrorCode = "BRAND003"; output.Message = ConstMessage.GetMsgConst("BRAND003"); return(output); } } try { var ProductBrandModel = _mapper.Map <ProductBrand>(model.Data); var ProdBrandId = await _repoWrapper.Brand.PostProductBrand(ProductBrandModel, model.ImgLogo, model.ImgBanner, model.UserId); if (ProdBrandId != 0) { // Save MainImage if (!String.IsNullOrEmpty(model.ImgLogo.Base64)) { await SaveLogoImage(model.ImgLogo, ProdBrandId); } //Save Sub Image if (!String.IsNullOrEmpty(model.ImgBanner.Base64)) { await SaveBannerImage(model.ImgBanner, ProdBrandId); } output.Data.ProductBrand_ID = ProdBrandId; ////Update Image //_repoWrapper.Brand.UpdateImgProductBrand(ProductBrandModel, model.ImgLogo, model.ImgBanner, ProdBrandId, model.UserId); _repoWrapper.FCMMessage.PushNotificationToRabitMQ(new NotificationRabitMQModel { Type = "ONDEMAND", NotificationCode = "DKCH", ChannelSend = "ALL", UsingTemplate = true, UserId = model.UserId, }); output.ErrorCode = "00"; output.Message = "Tạo gian hàng thành công"; } else { output.ErrorCode = "001"; output.Message = Utils.ConstMessage.GetMsgConst("001"); } } catch (Exception ex) { _logger.LogError($"PostProductBrand: " + ex.ToString()); output.ErrorCode = "001"; output.Message = Utils.ConstMessage.GetMsgConst("001"); } return(output); }
public async Task <object> Login([FromBody] LoginDto model) { //var username = principal.Identity.Name; //this is mapped to the Name claim by default var responseModel = new SumProfileResponseDTO(); responseModel.Profile = new ProfileReponse(); responseModel.ListRole = new List <ListRole>(); responseModel.Profile.Email = model.Email; var inputUserName = model.Email; var inputEmail = model.Email; if (Util.IsPhoneNumber(model.Email)) { model.Email = $"{model.Email}@hanoma.vn"; } //Check input Email Pass if (String.IsNullOrEmpty(model.Email) || String.IsNullOrEmpty(model.Password)) { responseModel.ErrorCode = "ACC006"; responseModel.Message = ConstMessage.GetMsgConst("ACC006"); return(responseModel); } else { //Check exists User var user = await _userManager.FindByNameAsync(inputUserName); if (user != null) { var result = await _signInManager.PasswordSignInAsync(inputUserName, model.Password, false, false); if (result.Succeeded) { //var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email); var aspNetUserLogin = _repositoryWrapper.AspNetUserProfiles.FirstOrDefault(p => p.UserId == user.Id); responseModel.Profile = _mapper.Map <ProfileReponse>(user); responseModel.Profile.FullName = aspNetUserLogin.FullName; responseModel.Profile.RegType = aspNetUserLogin.RegType; responseModel.Profile.AvartarFileName = aspNetUserLogin.AvatarUrl ?? "noimage.png"; responseModel.Profile.AvartarFullUrl = _configuration["Cloud_Path"] + $"/user/avatar/original/{aspNetUserLogin.AvatarUrl ?? "noimage.png"}"; //List Role var lstRole = await _repositoryWrapper.AspNetUsers.GetListRole(user.Id); responseModel.ListRole = _mapper.Map <List <ListRole> >(lstRole); //ProductBrandId var userProfiler = await _repositoryWrapper.AspNetUserProfiles.FirstOrDefaultAsync(p => p.UserId == user.Id); if (userProfiler != null) { responseModel.ProductBrandId = userProfiler.ProductBrand_ID ?? 0; if (responseModel.ProductBrandId != 0) { var brand = await _repositoryWrapper.Brand.FirstOrDefaultAsync(p => p.ProductBrand_ID == responseModel.ProductBrandId); if (brand != null) { responseModel.ProductBrandTypeId = brand.ProductBrandType_ID ?? 1; responseModel.ProductBrandYearJoin = (int)(DateTime.Now.Year - brand.CreateDate?.Year); responseModel.ReferralCode = brand.ReferralCode; responseModel.ProductBrandName = brand.Name; responseModel.ProductBrandAvatarUrl = _configuration["Cloud_Path"] + $"/productbrand/logo/original/{brand.Logo}"; } } } else { responseModel.ProductBrandId = 0; } // responseModel.Role = lstRole.Count > 0 ? string.Join(",", lstRole) : ""; var jwt = await GenerateJwtToken(inputUserName, user); responseModel.JWT = jwt.ToString(); responseModel.UserId = user.Id; responseModel.ErrorCode = "00"; responseModel.Message = "Đăng nhập thành công"; //Total UnRead responseModel.NumberFCMUnread = await _repositoryWrapper.FCMMessage.GetNumberFCMUnread(user.Id); return(responseModel); } // After register must verify if (result.IsNotAllowed) { responseModel.ErrorCode = "ACC013"; responseModel.Message = ConstMessage.GetMsgConst("ACC013"); return(responseModel); } //Yêu cầu xác thực mỗi lần đăng nhập if (result.RequiresTwoFactor) { responseModel.ErrorCode = "ACC013"; responseModel.Message = ConstMessage.GetMsgConst("ACC013"); return(responseModel); } if (result.IsLockedOut) { responseModel.ErrorCode = "ACC011"; responseModel.Message = ConstMessage.GetMsgConst("ACC011"); return(responseModel); } else { _logger.LogError($"[AccountController] Mật khẩu không đúng!"); responseModel.ErrorCode = "ACC007"; responseModel.Message = ConstMessage.GetMsgConst("ACC007"); return(responseModel); } } else { _logger.LogError($"[AccountController] Tài khoản {model.Email} không tồn tại"); responseModel.ErrorCode = "ACC008"; responseModel.Message = ConstMessage.GetMsgConst("ACC008"); return(responseModel); } } }
public async Task <object> Register([FromBody] RegisterDto model) { var reponseModel = new RegisterDto(); reponseModel.Email = model.Email; var InputEmail = model.Email; var code = ""; if (Util.IsPhoneNumber(model.Email)) { InputEmail = $"{model.Email}@hanoma.vn"; } //Check input Email Pass if (String.IsNullOrEmpty(model.Email) || String.IsNullOrEmpty(model.Password)) { reponseModel.ErrorCode = "ACC006"; reponseModel.Message = ConstMessage.GetMsgConst("ACC006"); return(reponseModel); } //Check exists User with not confirm if (_repositoryWrapper.AspNetUsers.CheckExistsUserNotConfirmed(model.Email)) { //reponseModel.ErrorCode = "ACC013"; //reponseModel.Message = ConstMessage.GetMsgConst("ACC013"); //SentCode if not confirm var user = new ApplicationUser { UserName = model.Email, Email = InputEmail }; //if (!Util.IsPhoneNumber(model.Email)) //{ // code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.Email); //} //else //{ // code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.Email); //} //await SendCode(Util.IsPhoneNumber(model.Email) ? "Phone" : "Email", model.Email, code); reponseModel.ErrorCode = "00"; reponseModel.Message = "Đã gửi code xác nhận"; return(reponseModel); } //Check exists User if (_repositoryWrapper.AspNetUsers.CheckExistsUser(model.Email)) { reponseModel.ErrorCode = "ACC009"; reponseModel.Message = ConstMessage.GetMsgConst("ACC009"); return(reponseModel); } if (!Util.IsEmailOrPhone(model.Email)) { _logger.LogError($"[AccountController] {model.Email}" + ConstMessage.GetMsgConst("ACC010")); reponseModel.ErrorCode = "ACC010"; reponseModel.Message = ConstMessage.GetMsgConst("ACC010"); return(reponseModel); } else { var user = new ApplicationUser { UserName = model.Email, Email = InputEmail }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { //Set EnableTwoFactorAuthentication //await _userManager.SetTwoFactorEnabledAsync(user, true); //Update Account Code var userRegister = _repositoryWrapper.AspNetUsers.FirstOrDefault(x => x.Id.Equals(user.Id)); userRegister.AccountCode = $"84{model.Email}"; _hanomaContext.SaveChanges(); _logger.LogInfo($"[AccountController] Đăng ký thành công {model.Email} "); try { //Create User Profile _repositoryWrapper.AspNetUsers.CreateNewUserProfile(user.Id, user.Email, null, 1); } catch (Exception ex) { _logger.LogError($"[Account Controller] {ex}"); } if (!Util.IsPhoneNumber(model.Email)) { code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.Email); } else { code = await _userManager.GenerateChangePhoneNumberTokenAsync(user, model.Email); } //Thay đổi luồng verify số điện thoại trước //await SendCode(Util.IsPhoneNumber(model.Email) ? "Phone" : "Email", model.Email, code); reponseModel.Email = model.Email; reponseModel.Password = model.Password; reponseModel.ErrorCode = "00"; reponseModel.Message = "Đăng ký thành công"; return(reponseModel); } } throw new ApplicationException("UNKNOWN_ERROR"); }
private async Task <object> LoginBySocial([FromBody] LoginSocialDto model) { var responseModel = new LoginSocialDto(); responseModel.Email = model.Email; responseModel.LoginProvider = model.LoginProvider; responseModel.ProviderKey = model.ProviderKey; if (Util.IsPhoneNumber(model.Email)) { model.Email = $"{model.Email}@hanoma.vn"; } //Check input Email Pass if (String.IsNullOrEmpty(model.Email)) { responseModel.ErrorCode = "ACC006"; responseModel.Message = ConstMessage.GetMsgConst("ACC006"); return(responseModel); } else { //Check exists User var userIDSocial = _repositoryWrapper.AspNetUsers.CheckExistsUserSocial(model.LoginProvider, model.ProviderKey); if (userIDSocial != null) { return(LoginSocialReturn(userIDSocial, model)); } else { if (Util.IsPhoneNumber(model.Email)) { model.Email = $"{model.Email}@hanoma.vn"; } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { _logger.LogInfo($"[AccountController] Đăng ký thành công {model.Email} "); try { _repositoryWrapper.AspNetUsers.CreateNewUserProfile(user.Id, user.Email, null, 1); //Create Social _repositoryWrapper.AspNetUsers.CreateUserSocial(model.LoginProvider, model.ProviderKey, user.Id); //return return(LoginSocialReturn(user.Id, model)); } catch (Exception ex) { _logger.LogError($"[Account Controller] {ex}"); return(LoginSocialReturn(null, model)); } } else { _logger.LogError($"[Account Controller] Error with Create Account Social"); return(LoginSocialReturn(null, model)); } } } }