public Envelope <dynamic> Create(dynamic data, UsuariosDS usuariosDS) { try { var result = usuariosDS.GetByUsuario(data); if (result.Result == "ok") { return(new Envelope <dynamic> { Result = "notSuccess", Message = "El Usuario ya Existe" }); } else { dynamic data2 = JsonConvert.DeserializeObject("{}"); data2.Nombre = data.Nombre; data2.Apellidos = data.Apellidos; data2.Email = data.Email; data2.Usuario = data.Usuario.ToString().ToLower(); data2.PasswordSalt = Guid.NewGuid().ToString(); data2.Password = HashHL.SHA256Of($"{data2.Usuario}123{data2.PasswordSalt}"); data2.IsActive = true; var response = usuariosDS.Insert(data2); return(response); } } catch (Exception ex) { return(new Envelope <dynamic>() { Result = "error", Message = ex.Message }); } }
public Envelope CreateAdmin(dynamic data, UsuariosDS usuariosDS) { try { var adminUserExist = usuariosDS.GetByUsuario(new { Usuario = "admin" }); if (adminUserExist.Result == "empty") { dynamic data2 = JsonConvert.DeserializeObject("{}"); data2.Usuario = "admin"; data2.Password = "******"; data2.Nombre = "Administrator"; data2.Apellidos = "del Sistema"; data2.Email = ""; data2.IsActive = true; var result = this.Create(data2, usuariosDS); return(result); } else { return(new Envelope() { Result = "notSuccess", Message = "Admin user already exist." }); } } catch (Exception ex) { return(new Envelope() { Result = "error", Message = ex.Message }); } }
public Envelope <dynamic> Login(dynamic data, UsuariosDS usuariosDS) { string usuario = data.Usuario; string password = data.Password; var response = usuariosDS.GetByUsuario(new { Usuario = usuario.ToLower() }); if (response.Result == "empty") { return(new Envelope <dynamic>() { Result = "notSuccess", Message = "Usuario o Password no encontrado" }); } var dataSet = response.Data.FirstOrDefault(); if (HashHL.SHA256Of($"{usuario}{password}{dataSet.PasswordSalt}") != dataSet.Password && dataSet.Password != password) { return(new Envelope <dynamic>() { Result = "notSuccess", Message = "Usuario o Password no encontrado" }); } else { var signingKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(plainTextSecurityKey)); var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256Signature); var x = new List <Claim>() { new Claim(ClaimTypes.NameIdentifier, usuario), new Claim(ClaimTypes.Name, dataSet.Nombre), new Claim(ClaimTypes.Surname, dataSet.Apellidos) }; var claimsIdentity = new ClaimsIdentity(x, "Custom"); var securityTokenDescriptor = new SecurityTokenDescriptor() { Audience = "http://localhost:61101", Issuer = "http://localhost:61101", Subject = claimsIdentity, Expires = DateTime.Now.AddHours(12), SigningCredentials = signingCredentials, }; var tokenHandler = new JwtSecurityTokenHandler(); var plainToken = tokenHandler.CreateToken(securityTokenDescriptor); var signedAndEncodedToken = tokenHandler.WriteToken(plainToken); dynamic dataReturn = new { Token = signedAndEncodedToken, Usuario = usuario, dataSet.Nombre, dataSet.Apellidos, dataSet.Email }; return(new Envelope <dynamic>() { Result = "ok", Data = dataReturn }); } }