Beispiel #1
0
        public async Task <IActionResult> Student(StudentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var email      = model.Email;
                var password   = model.Password;
                var token      = model.Token;
                var longtitude = model.Longitude;
                var latitude   = model.Latitude;
                var jsonObj    = new { email, password, token, longtitude, latitude };

                using (HttpClient client = new HttpClient())
                {
                    var response = await client.PostAsJsonAsync("http://localhost:11743/account/studentlogin", jsonObj);

                    var resultString = await response.Content.ReadAsStringAsync();

                    using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(resultString)))
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(LoginApiData));
                        LoginApiData obj = (LoginApiData)serializer.ReadObject(stream);

                        if (obj.Succeded)
                        {
                            ViewData["Success"] = "You have succesfilly checked in..";
                            var modell = new StudentViewModel();
                            return(View(modell));
                        }
                        else
                        {
                            ViewData["Error"] = obj.Response;
                            return(View(model));
                        }
                    }
                }
            }
            var error = ModelState.Values.FirstOrDefault(x => x.ValidationState == ModelValidationState.Invalid).Errors.First().ErrorMessage;

            ViewData["Error"] = "I have failed you, and for that i am sorry: " + error;
            return(View(model));
        }
Beispiel #2
0
        public async Task <IActionResult> Index(IndexViewModel model)
        {
            if (ModelState.IsValid)
            {
                var email    = model.Email;
                var password = model.Password;
                var jsonObj  = new { email, password };

                using (HttpClient client = new HttpClient())
                {
                    var response = await client.PostAsJsonAsync("http://localhost:11743/account/teacherlogin", new { email, password });

                    var resultString = await response.Content.ReadAsStringAsync();

                    using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(resultString)))
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(LoginApiData));
                        LoginApiData obj = (LoginApiData)serializer.ReadObject(stream);

                        if (obj.Succeded)
                        {
                            HttpContext.Session.SetString("token", obj.Response);
                            return(RedirectToAction("Index", "Teacher"));
                        }
                        else
                        {
                            ViewData["Error"] = obj.Response;
                            return(View(model));
                        }
                    }
                }
            }
            var error = ModelState.Values.FirstOrDefault(x => x.ValidationState == ModelValidationState.Invalid).Errors.First().ErrorMessage;

            ViewData["Error"] = error;
            return(View(model));
        }