public async Task <IActionResult> Reservation(Reservation reservation)
        {
            var manu = Request.Form["manu"];

            reservation.ManuType = manu;
            try
            {
                if (!ModelState.IsValid)
                {
                    return(RedirectToAction("Index", "Home"));
                }
                HttpClient httpClient = UserAPI.Initail();
                var        token      = HttpContext.Session.GetString("Token");
                if (token == null)
                {
                    return(RedirectToAction("Login", "Account"));
                }
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                var reg                 = JsonConvert.SerializeObject(reservation);
                var content             = new StringContent(reg.ToString(), Encoding.UTF8, "application/json");
                HttpResponseMessage res = httpClient.PostAsync("api/Reservation", content).Result;
                if (res.IsSuccessStatusCode)
                {
                    var result     = res.Content.ReadAsStringAsync().Result;
                    var JsonResult = JsonConvert.DeserializeObject <JsonResultViewModel>(result);
                    if (JsonResult.result == "Sucessfully Register")
                    {
                        ViewBag.Reservation = "Sucessfully Reservation";
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Unable to authorize to access the api");
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Unable to authorize to access the api");
                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to authorize to access the api");
                return(RedirectToAction("Index", "Home"));
            }
        }
Esempio n. 2
0
        public IActionResult Login(Login login)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(login));
                }
                HttpClient          httpClient = UserAPI.Initail();
                var                 reg        = JsonConvert.SerializeObject(login);
                var                 content    = new StringContent(reg.ToString(), Encoding.UTF8, "application/json");
                HttpResponseMessage res        = httpClient.PostAsync("api/Account/Login", content).Result;
                if (res.IsSuccessStatusCode)
                {
                    var result     = res.Content.ReadAsStringAsync().Result;
                    var JsonResult = JsonConvert.DeserializeObject <LoginCustomjsonResult>(result);
                    if (JsonResult.Result == "Invalid login attempt")
                    {
                        ModelState.AddModelError("", "Invalid login attempt");
                        return(View(login));
                    }
                    else
                    {
                        HttpContext.Session.SetString(SessionUserName, JsonResult.Email);
                        HttpContext.Session.SetString(SessionToken, JsonResult.Token);

                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Unable to authorize to access the api");
                    return(View(login));
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to authorize to access the api");
                return(View(login));
            }
        }
Esempio n. 3
0
 public async Task <IActionResult> Register(Registration registration)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(View(registration));
         }
         HttpClient          httpClient = UserAPI.Initail();
         var                 reg        = JsonConvert.SerializeObject(registration);
         var                 content    = new StringContent(reg.ToString(), Encoding.UTF8, "application/json");
         HttpResponseMessage res        = httpClient.PostAsync("api/Account/Register", content).Result;
         if (res.IsSuccessStatusCode)
         {
             var result     = res.Content.ReadAsStringAsync().Result;
             var JsonResult = JsonConvert.DeserializeObject <JsonResultViewModel>(result);
             if (JsonResult.result == "Error")
             {
                 ModelState.AddModelError("", "Unable to Create User");
                 return(View(registration));
             }
             else
             {
                 return(RedirectToAction("Login"));
             }
         }
         else
         {
             ModelState.AddModelError("", "Unable to authorize to access the api");
             return(View(registration));
         }
     }
     catch
     {
         ModelState.AddModelError("", "Unable to authorize to access the api");
         return(View(registration));
     }
 }