Ejemplo n.º 1
0
        public IActionResult Index1()
        {
            //return View();
            Userpass obj = new Userpass {
                Usern = "admin", Pass = "******"
            };

            using (HttpClient client = new HttpClient())
            {
                var token = GetToken("https://localhost:44326/api/Token", obj);
                client.BaseAddress = new Uri("https://localhost:44394/api/");
                // MediaTypeWithQualityHeaderValue contentType = new MediaTypeWithQualityHeaderValue("application/json");
                // client.DefaultRequestHeaders.Accept.Add(contentType);
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = new HttpResponseMessage();
                response = client.GetAsync("Receivers").Result;

                string stringData = response.Content.ReadAsStringAsync().Result;

                var data = JsonConvert.DeserializeObject <IEnumerable <Receiver> >(stringData);

                return(View(data));
            }
        }
        static string GetToken(string url, Userpass user)
        {
            var json = JsonConvert.SerializeObject(user);
            var data = new StringContent(json, Encoding.UTF8, "application/json");

            using (var client = new HttpClient())
            {
                var     response = client.PostAsync(url, data).Result;
                string  name     = response.Content.ReadAsStringAsync().Result;
                dynamic details  = JObject.Parse(name);
                return(details.token);
            }
        }
        public IActionResult Login(Userpass user)
        {
            string token = GetToken("https://localhost:44326/api/Token", user);

            // string token = GetToken("http://52.191.222.87/api/Token", user);
            if (token != null)
            {
                return(RedirectToAction("Index", "DonorProj", new { name = token }));
            }
            else
            {
                ViewBag.invalid = "UserId or Password invalid";
                return(View());
            }
        }