public bool?ResetPassword(PasswordModel model) { try { var user = _repo.GetActive().Where(p => p.Email == model.Email).FirstOrDefault(); if (user == null) { return(null); } var token = _aspNetUserTokensRepo.Get().Where(p => p.UserId == user.Id && p.Name == user.Name && p.Value == model.Token).FirstOrDefault(); if (token == null) { return(false); } UniLogUtil utils = new UniLogUtil(); user.PasswordHash = utils.GetMd5HashData(model.NewPassword); _repo.Update(user); _repo.SaveChanges(); return(true); } catch (Exception) { throw; } }
public LoginResponseModel Login(AuthorizeLoginModel loginModel) { try { var account = _repo.GetActive().Where(p => p.Email == loginModel.Email).FirstOrDefault(); if (account == null) { return(null); } if (!_IAuthorizeService.Authenticate(loginModel.Email, loginModel.Password)) { return(null); } UniLogUtil utils = new UniLogUtil(); var aspUser = _aspNetUsersRepository.GetActive().Where(p => p.Email == loginModel.Email && p.PasswordHash == utils.GetMd5HashData(loginModel.Password)).FirstOrDefault(); if (aspUser == null) { return(null); } var result = new LoginResponseModel(); result.Id = account.Id; result.Token = aspUser.SecurityStamp; result.Role = account.Role; result.Email = account.Email; return(result); } catch (Exception) { throw; } }
public string CreateToken(string str) { if (string.IsNullOrEmpty(str)) { return(""); } UniLogUtil utils = new UniLogUtil(); string token = ""; int length = new Random().Next(1, str.Length); for (int i = 0; i < length; i++) { var dateString = DateTime.Now.Millisecond.ToString(); token += str.Replace(str[new Random().Next(1, str.Length - 1)], dateString[new Random().Next(1, dateString.Length - 1)]); } token = utils.GetMd5HashData(token); return(token); }
public bool?ChangePassword(AspNetUsersPartialUpdateRequestModel requestModel) { try { var user = _repo.GetActive().Where(p => p.Email == requestModel.Email).FirstOrDefault(); if (user == null) { return(null); } UniLogUtil utils = new UniLogUtil(); user.PasswordHash = utils.GetMd5HashData(requestModel.NewPassword); _repo.Update(user); _repo.SaveChanges(); return(true); } catch (Exception) { throw; } }
public string GetLoginProvider(string email, string password) { try { UniLogUtil utils = new UniLogUtil(); password = utils.GetMd5HashData(password); var user = _aspNetUsersRepository.GetActive().Where(x => x.Email == email && x.PasswordHash == password).FirstOrDefault(); // return null if user not found if (user == null) { return(null); } var token = _aspNetUserTokensRepository.Get().Where(x => x.Name == user.Name && x.UserId == user.Id).FirstOrDefault().LoginProvider; return(token); } catch (Exception) { throw; } }
public bool Authenticate(string email, string password) { try { UniLogUtil utils = new UniLogUtil(); password = utils.GetMd5HashData(password); var user = _aspNetUsersRepository.GetActive().Where(x => x.Email == email && x.PasswordHash == password).FirstOrDefault(); // return null if user not found if (user == null) { return(false); } // authentication successful so generate jwt token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Id.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); user.SecurityStamp = tokenHandler.WriteToken(token); return(true); } catch (Exception) { throw; } }
public override AccountServiceModel Create(AuthorizeRegisterModel requestModel) { try { // Check existed email var existAccount = _repo.GetActive().Where(p => p.Email == requestModel.Email).FirstOrDefault(); if (existAccount != null) { return(null); } // Check Administrator / BrandManager / Employee Register if (requestModel.ManagerRegistrationToken == "token_v2_7/2019") { if (requestModel.IsAdmin) { requestModel.Role = 2; } else { requestModel.Role = 3; } } else { requestModel.Role = 1; } // Create AspNetUser UniLogUtil utils = new UniLogUtil(); var accountNetUser = Mapper.Map <AuthorizeRegisterModel, AspNetUsersCreateRequestModel>(requestModel); accountNetUser.PasswordHash = utils.GetMd5HashData(requestModel.Password); accountNetUser.NormalizedUserName = accountNetUser.Name.ToUpper(); accountNetUser.NormalizedEmail = accountNetUser.Email.ToUpper(); accountNetUser.NormalizedUserName = accountNetUser.Name.ToUpper(); accountNetUser.PhoneNumber = requestModel.Phone; var aspNetUser = _aspNetUsersService.Create(accountNetUser); // Create AspNetUserTokens AspNetUserTokens aspToken = new AspNetUserTokens() { UserId = aspNetUser.Id, Name = aspNetUser.Name, LoginProvider = _aspNetUsersService.CreateToken(aspNetUser.Email) }; _aspNetUserTokensRepo.Create(aspToken); _aspNetUserTokensRepo.SaveChanges(); // Create AspNetUsersRoles AspNetUserRoles aspUserRoles = new AspNetUserRoles() { UserId = aspNetUser.Id, RoleId = requestModel.Role }; _aspNetUserRolesRepository.Create(aspUserRoles); _aspNetUserRolesRepository.SaveChanges(); // Create Account var account = Mapper.Map <AuthorizeRegisterModel, Account>(requestModel); account.AspNetUserId = aspNetUser.Id; _repo.Create(account); _repo.SaveChanges(); return(Mapper.Map <Account, AccountServiceModel>(account)); } catch (Exception) { throw; } }