public async Task <Response <Usuario> > UsuarioValidoAsync(Usuario user) { var resp = new Response <Usuario>(); resp.Return = null; using (var client = this._context.GetClientInstance()) { QueryRequest request = ObterUsuarioQueryRequest("login", new AttributeValue { S = user.Login }); QueryResponse response = null; try { response = await client.QueryAsync(request); List <Usuario> lstUser = ExtractUserFrom(response.Items); if (lstUser != null && lstUser.Count > 0) { var userDb = lstUser[0]; var hash = SecurityCrypt.GenerateHash(user.Senha + userDb.Salt); if (hash == userDb.HashedPassword) { resp.Return = userDb; } } return(resp); } catch (Exception e) { resp.ErrorMessages.Add(e.Message); _log.LogError(e.Message); return(resp); } } }
public async Task <Response <Usuario> > SalvarAsync(Usuario user) { var resp = new Response <Usuario>(); using (var client = this._context.GetClientInstance()) { try { StringBuilder updExp = new StringBuilder("SET "); var exprAttrValues = new Dictionary <string, AttributeValue>(); var exprAttrNames = new Dictionary <string, string>(); if (user.Id < 1) { user.Id = (Int32)DateTimeOffset.UtcNow.ToUnixTimeSeconds(); } user.DtAtualizacao = DateTime.Now; exprAttrValues.Add(":dtAt", new AttributeValue { S = user.DtAtualizacao.Value.ToString("dd/MM/yyyy hh:mm:ss") }); updExp.Append(" #dtAt = :dtAt,"); exprAttrNames.Add("#dtAt", "dt-atualizacao"); if (user.Sexo.HasValue) { exprAttrValues.Add(":sexo", new AttributeValue { S = user.Sexo.ToString() }); updExp.Append(" #sexo = :sexo,"); exprAttrNames.Add("#sexo", "sexo"); } if (!String.IsNullOrEmpty(user.Senha)) { var salt = SecurityCrypt.GenerateSalt(); exprAttrValues.Add(":salt", new AttributeValue { S = salt }); updExp.Append(" #salt = :salt,"); exprAttrNames.Add("#salt", "salt"); var hash = SecurityCrypt.GenerateHash(user.Senha + salt); exprAttrValues.Add(":hash", new AttributeValue { S = hash }); updExp.Append(" #hash = :hash,"); exprAttrNames.Add("#hash", "hashedPassword"); } if (!String.IsNullOrEmpty(user.Login)) { exprAttrValues.Add(":login", new AttributeValue { S = user.Login }); updExp.Append(" #login = :login,"); exprAttrNames.Add("#login", "login"); } if (!String.IsNullOrEmpty(user.Nome)) { exprAttrValues.Add(":nome", new AttributeValue { S = user.Nome }); updExp.Append(" #nome = :nome,"); exprAttrNames.Add("#nome", "nome"); } if (user.DtNascimento.HasValue) { exprAttrValues.Add(":dtNasc", new AttributeValue { S = user.DtNascimento.ToString() }); updExp.Append(" #dtNasc = :dtNasc,"); exprAttrNames.Add("#dtNasc", "dt-nascimento"); } if (!String.IsNullOrEmpty(user.Email)) { exprAttrValues.Add(":email", new AttributeValue { S = user.Email }); updExp.Append(" #email = :email,"); exprAttrNames.Add("#email", "email"); } if (!String.IsNullOrEmpty(user.Celular)) { exprAttrValues.Add(":celular", new AttributeValue { S = user.Celular }); updExp.Append(" #celular = :celular,"); exprAttrNames.Add("#celular", "celular"); } if (!String.IsNullOrEmpty(user.UrlFoto)) { exprAttrValues.Add(":urlFoto", new AttributeValue { S = user.UrlFoto }); updExp.Append(" #urlFoto = :urlFoto,"); exprAttrNames.Add("#urlFoto", "url-foto"); } if (!String.IsNullOrEmpty(user.Observacao)) { exprAttrValues.Add(":obs", new AttributeValue { S = user.Observacao }); updExp.Append(" #obs = :obs,"); exprAttrNames.Add("#obs", "obs"); } var request = new UpdateItemRequest { TableName = _context.TableName, Key = new Dictionary <string, AttributeValue> { { "tipo", new AttributeValue { S = "usuario" } }, { "id", new AttributeValue { N = user.Id.ToString() } } }, ExpressionAttributeNames = exprAttrNames, ExpressionAttributeValues = exprAttrValues, UpdateExpression = updExp.ToString().Substring(0, updExp.ToString().Length - 1) }; var updResp = await client.UpdateItemAsync(request); resp.Return = user; return(resp); } catch (Exception e) { resp.Return = user; resp.ErrorMessages.Add(e.Message); _log.LogError(e.Message); return(resp); } } }