/// <summary> /// Adds and Account. Doesn't catch exceptions if Username already exists /// </summary> /// <param name="account"></param> /// <param name="saltLength"></param> public static void AddAccount(this DbSet <HashedPWAccount> dbset, Account account, int saltLength = 512) { var salt = Salt.GenerateSalt(saltLength); HashedPWAccount hashedPWAccount = new HashedPWAccount { User = account.User, Salt = salt, Hash = SHA256Utility.ComputeHash(account.Password + salt) }; dbset.Add(hashedPWAccount); }
public ActionResult AccountCreation(EmployeeVM employee) { EmployeeMap.CreateEmployee(employee); employee.EmployeeId = EmployeeMap.GetEmployeeId(employee.EmployeeNumber); employee.Login.Email = employee.Address.Email; employee.Login.Salt = Convert.ToBase64String(Salt.GenerateSalt()); employee.Login.Password = ORA_Data.Hash.GetHash(employee.Login.Password + employee.Login.Salt); LoginDAL.Register(Mapper.Map <LoginDM>(employee.Login), employee.EmployeeId); AddressDAL.CreateAddress(Mapper.Map <AddressDM>(employee.Address), employee.EmployeeId); Work_StatusDAL.CreateStatus(Mapper.Map <StatusDM>(employee.Status), employee.EmployeeId); TimeDAL.CreateEmptyTime(employee.EmployeeId); return(View()); }
public string GetAuthToken(HttpContext httpContext) { var userId = httpContext.User.FindFirst (claim => claim.Type == ClaimTypes.NameIdentifier) .Value; byte[] salt = Salt.GenerateSalt(); string authToken = System.Guid.NewGuid().ToString("N"); string hashedAuthToken = Hash.Compute(authToken, salt); hashedAuthToken = TrimToken(hashedAuthToken); UserResult userResult = UserProcessor.FindByField("id", userId).SingleOrDefault(); ApiSessionResult sessionResult = CreateApiSession(userResult, hashedAuthToken); return(sessionResult.AuthToken); }
static void Main(string[] args) { // var connect = new DatabaseConnection(); string senha = "1"; while (senha != "0") { Console.WriteLine("Qual a senha que deseja encriptar? "); senha = Console.ReadLine(); var hash = Hash.GenerateHash(senha); var salt = Salt.GenerateSalt(); var encrypt = Program.JoinHashSalt(salt, hash); Console.WriteLine($"SENHA: {Convert.ToBase64String(hash)}"); Console.WriteLine($"SALT: {Convert.ToBase64String(salt)}"); Console.WriteLine($"ENCRYPT FINAL: {Convert.ToBase64String(encrypt)}"); } }
public ActionResult Register(LoginVM info) { try { if (info.Password != info.ConfirmPassword) { return(View()); } info.Salt = Convert.ToBase64String(Salt.GenerateSalt()); info.Password = ORA_Data.Hash.GetHash(info.Password + info.Salt); //LoginDAL.Register(Mapper.Map<LoginDM>(info)); info.Password = ""; return(ConfigurationManager.AppSettings["RegisterToLogin"].ToLower() == "true" ? RedirectToAction("Login", "Login", info) : RedirectToAction("Home", "Index", new { area = "Default" })); } catch (Exception ex) { throw ex; } }
/// <summary> /// Responsável por efetuar o registro de novos usuários na aplicação /// </summary> /// <param name="entidade"> Entidade contendo informações do novo usuário à ser registrado</param> public void Registrar(Usuario entidade) { try { var entidadeLocalizada = _dbContext.Usuarios.Where(data => data.Login.Equals(entidade.Login, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); if (entidadeLocalizada != null) { throw new RegistroDuplicadoException("Usuário já existe na aplicação"); } Salt _Salt = new Salt(_LengthSalt); JavaScriptSerializer js = new JavaScriptSerializer(); byte[] SaltDeSerializado = _Salt.GenerateSalt(); string SerializeSalt = js.Serialize(SaltDeSerializado); byte[] result = _Salt.GenerateDerivedKey(_LengthSalt, Encoding.UTF8.GetBytes(entidade.Senha), SaltDeSerializado, 5000); entidade.Login = entidade.Email; entidade.Senha = _Salt.getPassword(result); entidade.Salt = SerializeSalt; entidade.Claim = "USER"; if (string.IsNullOrWhiteSpace(entidade.TimeZone) && (HelpTimeZone.IsIDUTCValid(entidade.TimeZone))) { entidade.TimeZone = HelpTimeZone.RecuperarIDTimeZonePadrao(); } entidade.Bloqueado = true; entidade.DataInclusao = DateTime.UtcNow; entidade.DataBloqueio = DateTime.UtcNow; entidade.Verificado = false; _dbContext.Usuarios.Add(entidade); _dbContext.SaveChanges(); entidade.UsuarioInclusao = entidade.ID; _dbContext.SaveChanges(); } catch (DbEntityValidationException ex) { var st = new StringBuilder(); ex.EntityValidationErrors.ToList().ForEach(errs => errs.ValidationErrors.ToList().ForEach(err => st.AppendLine(err.ErrorMessage))); //TODO AJUSTAR O EXCEPTION DO RETORNO throw new Exception(st.ToString(), ex); } }