Example #1
0
        // GET: Usuario
        public async System.Threading.Tasks.Task <ActionResult> Index()
        {
            IEnumerable <Cidade> cidade = await GetCidadesAsync();

            IEnumerable <Usuario> usuarios = null;


            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:55343/api/usuario");
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));


                string token = await AutenticacaoUsuarios.getTokenAsync();

                client.DefaultRequestHeaders.Add("Authorization", "bearer " + token);

                HttpResponseMessage resposta = await client.GetAsync(client.BaseAddress.ToString());

                if (resposta.IsSuccessStatusCode)
                {
                    var conteudo = resposta.Content.ReadAsStringAsync().Result;
                    usuarios = JsonConvert.DeserializeObject <Usuario[]>(conteudo);
                }
                return(View(usuarios));
            }
        }
Example #2
0
        public async System.Threading.Tasks.Task <ActionResult> Delete(int id, Usuario usuario)
        {
            try
            {
                if (id != usuario.cod_cliente)
                {
                    return(HttpNotFound());
                }

                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:55343/");
                    client.DefaultRequestHeaders.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));


                    string token = await AutenticacaoUsuarios.getTokenAsync();

                    client.DefaultRequestHeaders.Add("Authorization", "bearer " + token);



                    await client.DeleteAsync("api/usuarios/" + id);


                    return(RedirectToAction("Index"));
                }
            }
            catch
            {
                return(View());
            }
        }
Example #3
0
        public async System.Threading.Tasks.Task <ActionResult> Create(Usuario usuario)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.cod_cidade = new SelectList
                                         (
                        cidade,
                        "cod_cidade",
                        "nome_cidade",
                        usuario.cod_cidade
                                         );

                    return(View(usuario));
                }


                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:55343/api/usuario");
                    client.DefaultRequestHeaders.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));


                    string token = await AutenticacaoUsuarios.getTokenAsync();

                    client.DefaultRequestHeaders.Add("Authorization", "bearer " + token);

                    await client.PostAsJsonAsync(client.BaseAddress.ToString(), usuario);


                    return(RedirectToAction("Index"));
                }
            }
            catch
            {
                return(View());
            }

            //return View();
        }