public void Should_create_salt() { ICryptographer cryptographer = new Cryptographer(); var salt = cryptographer.CreateSalt(); Assert.That(salt.Length, Is.EqualTo(88)); }
public Task <bool> Handle(UsersInsertCommand request, CancellationToken cancellationToken) { if (request.IsValid()) { try { using (var connection = _usersRepository.GetFirstConnection()) { if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } var transaction = connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted); var model = _usersRepository.Find("select * from Users where mobile = @mobile", new { @mobile = request.Users.Mobile }, transaction); if (model != null) { ///事件收集 _bus.RaiseEvent(new DomainNotification("", "该用户已存在!")); } else { var passwordSalt = Cryptographer.CreateSalt(); var password = Cryptographer.EncodePassword(request.Users.Password, 1, passwordSalt); var result = _usersRepository.Insert(new UsersEntities(request.Users.Id, request.Users.UserName, password, request.Users.Name, request.Users.Email, DateTime.Now, request.Users.Mobile, (int)CommonState.正常, request.Users.PicUrl, passwordSalt), transaction); transaction.Commit(); if (result != null) { return(Task.FromResult(true)); } return(Task.FromResult(false)); } } } catch (Exception ex) { throw; } } else { //验证不通过 NotifyValidationErrors(request); } return(Task.FromResult(false)); }
static void Main(string[] args) { ICryptographer cryptographer = new Cryptographer(); var rawString = "Hello World"; var salt = cryptographer.CreateSalt(); var encryptedString = cryptographer.Encrypt(rawString, salt); WriteLine($"Encrypted String - {encryptedString}"); WriteLine(); var decryptedString = cryptographer.Decrypt(encryptedString, salt); WriteLine($"Decrypted String - {decryptedString}"); WriteLine(); ReadLine(); }
public IActionResult Reset(ResetPwd reset) { if (ModelState.IsValid) { var model = _repositoryEF.Find(reset.Id); if (model == null) { return(Json(new { Data = false })); } else { model.Encrypt = Cryptographer.CreateSalt(); model.Password = Cryptographer.EncodePassword(reset.Pwd, 1, model.Encrypt); _repositoryEF.Update(model); _unitOfWork.Commit(); return(Json(new { Data = true })); } } return(Json(new { Data = false })); }
private User[] CreateUsers() { var crypto = new Cryptographer(); string salt = crypto.CreateSalt(); return(new[] { new User { Name = "Joe User", Username = "******", EmailAddress = "*****@*****.**", PasswordHash = crypto.GetPasswordHash("password", salt), PasswordSalt = salt, }, new User { Name = "Jeffrey Palermo", EmailAddress = "*****@*****.**", Username = "******", PasswordHash = crypto.GetPasswordHash("beer", salt), PasswordSalt = salt, }, new User { Name = "Homer Simpson", EmailAddress = "*****@*****.**", Username = "******", PasswordHash = crypto.GetPasswordHash("beer", salt), PasswordSalt = salt, }, new User { Name = "Bart Simpson", EmailAddress = "*****@*****.**", Username = "******", PasswordHash = crypto.GetPasswordHash("beer", salt), PasswordSalt = salt, } }); }
public async Task <IActionResult> Add(ManagerViewModel entities) { if (ModelState.IsValid) { var isTrue = _repositoryEF.Count(s => s.Account.Equals(entities.Account) || s.Mobile.Equals(entities.Mobile)) > 0; if (isTrue) { ModelState.AddModelError("Account", "该账户或联系电话已存在"); return(View(entities)); } var entity = _mapper.Map <ManagerEntities>(entities); entity.Status = (int)CommonState.正常; entity.CreateTime = DateTime.Now; entity.Encrypt = Cryptographer.CreateSalt(); entity.Password = Cryptographer.EncodePassword("123qwe", 1, entity.Encrypt); await _repositoryEF.InsertAsync(entity); _unitOfWork.Commit(); return(Json(new { d = true })); } return(Json(new { d = false })); }
public IHttpActionResult Register(RegisterRepresentation registerRepresentation) { var confirmationUrl = new Uri(Request.RequestUri, "/confirmation"); if (ModelState.IsValid) { if (!Regex.IsMatch(registerRepresentation.Password, @"(?!.*\s)[0-9a-zA-Z!@#\\$%*()_+^&}{:;?.]*$")) { ModelState.AddModelError("password", "The password does not match the policy"); return(BadRequest(ModelState)); } IHttpActionResult response = Ok(); // Here we shoud have set up a Created but would need and URL for created ressource. using (var ctx = new RegistrationContext()) { if (ctx.Accounts.Any(a => a.Email == registerRepresentation.Email)) { return(Conflict()); } var cryptographer = new Cryptographer(); string cryptedPassword = cryptographer.GetPasswordHash( registerRepresentation.Password, cryptographer.CreateSalt()); var account = new Account { Email = registerRepresentation.Email, Password = cryptedPassword, Provider = registerRepresentation.Provider }; account.ConfirmEmail(DateTime.Now); if (!account.IsEmailConfirmed) { var expirationEndDate = DateTime.Now.AddDays(5); account.SetActivationCode(Guid.NewGuid(), expirationEndDate); var notifier = new Notifier(); notifier.SendActivationNotification(account.Email); response = Created(confirmationUrl, new ConfirmationRepresentation { Email = account.Email, Code = account.ActivationCode, ExpirationTime = account.ActivationCodeExpirationTime.GetValueOrDefault(expirationEndDate) }); } ctx.Accounts.Add(account); ctx.SaveChanges(); return(response); } } return(BadRequest(ModelState)); }