Beispiel #1
0
        /// <summary>
        /// Copies headers from one <see cref="System.Net.Http.HttpResponseMessage"/> instance to another.
        /// </summary>
        /// <param name="source">The source <see cref="System.Net.Http.HttpResponseMessage"/> to copy from.</param>
        /// <param name="destination">The destination <see cref="System.Net.Http.HttpResponseMessage"/> to copy to.</param>
        public static void CopyHeadersTo(this System.Net.Http.Headers.HttpResponseHeaders source, System.Net.Http.Headers.HttpResponseHeaders destination)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            foreach (var header in source)
            {
                if (destination.Contains(header.Key))
                {
                    destination.Remove(header.Key);
                }

                destination.Add(header.Key, header.Value);
            }
        }
        private async Task <bool> ValidateLogin()
        {
            client.BaseAddress = CommonData.ApiUrl;

            var jsonString = JsonConvert.SerializeObject(model);
            var content    = new StringContent(jsonString, Encoding.UTF8, "application/json");
            IEnumerable <string> found;

            try
            {
                HttpResponseMessage responseFromRequest = await client.PostAsync("api/ExternalLogin", content);

                if (responseFromRequest != null)
                {
                    System.Net.Http.Headers.HttpResponseHeaders headers = responseFromRequest.Headers;
                    if (headers.Contains("Found"))
                    {
                        if (headers.TryGetValues("Found", out found) && found.ToArray()[0] == "true")
                        {
                            if (headers.Contains("EmployeeID"))
                            {
                                EmployeeID              = headers.GetValues("EmployeeID").ToString();
                                Session["UserName"]     = model.UserName;
                                Session["SafetyLogged"] = true;
                                return(true);
                            }
                            else if (headers.Contains("WrongPassword"))
                            {
                                ViewBag.Error = "Password doesn't match.";
                                return(false);
                            }
                        }
                        else if (headers.TryGetValues("Found", out found) && found.ToArray()[0] == "false")
                        {
                            if (headers.Contains("UserNotFound"))
                            {
                                ViewBag.Error = "Username not found";
                                return(false);
                            }
                            else if (headers.Contains("WrongPassword"))
                            {
                                ViewBag.Error = "Password incorrect";
                                return(false);
                            }
                        }
                    }
                }
                ViewBag.Error = "Internal issue, please try again later.";
                return(false);
            }
            catch (Exception exc)
            {
                Response.Write("<script>alert('" + Server.HtmlEncode(exc.Message) + "')</>");
                return(false);
            }


            #region validationUsingEntFramework
            //parkingEntities entity = new parkingEntities();

            ////using linq to find user
            //var result = entity.SystemUsers.FirstOrDefault(m => m.LoginUserName == loginData.UserName);

            //if (result != null)
            //{
            //    var userAndPass = entity.SystemUsers.FirstOrDefault(m => m.LoginUserName == loginData.UserName && m.LoginPassword == loginData.Password);

            //    if(userAndPass != null)
            //    {
            //        Session["UserName"] = result.LoginUserName;
            //        Session["SafetyLogged"] = true;
            //        return RedirectToAction("Index", "Home");
            //    }
            //    else
            //    {
            //        ViewBag.WrongData = "Wrong password.";
            //        return View("Login");
            //    }
            //}
            //else
            //{
            //    ViewBag.LoginError = "User ot found.";
            //    return View("Login");
            //}

            #endregion
        }
        public async Task <ActionResult> Login(LoginM data)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            model = data;
            client.BaseAddress = CommonData.ApiUrl;

            var jsonString = JsonConvert.SerializeObject(model);
            var content    = new StringContent(jsonString, Encoding.UTF8, "application/json");
            IEnumerable <string> found;

            try
            {
                HttpResponseMessage responseFromRequest = await client.PostAsync("api/ExternalLogin", content);

                if (responseFromRequest != null)
                {
                    System.Net.Http.Headers.HttpResponseHeaders headers = responseFromRequest.Headers;
                    if (headers.Contains("Found"))
                    {
                        if (headers.TryGetValues("Found", out found) && found.ToArray()[0] == "true")
                        {
                            if (headers.Contains("EmployeeID"))
                            {
                                EmployeeID              = headers.GetValues("EmployeeID").ToString();
                                Session["UserName"]     = model.UserName;
                                Session["SafetyLogged"] = true;
                                return(RedirectToAction("", ""));
                            }
                            else if (headers.Contains("WrongPassword"))
                            {
                                ViewBag.Error = "Password doesn't match.";
                                return(RedirectToAction("Login", "Login"));
                            }
                        }
                        else if (headers.TryGetValues("Found", out found) && found.ToArray()[0] == "false")
                        {
                            if (headers.Contains("UserNotFound"))
                            {
                                ViewBag.Error = "Username not found";
                                return(RedirectToAction("", ""));
                            }
                            else if (headers.Contains("WrongPassword"))
                            {
                                ViewBag.Error = "Password incorrect";
                                return(RedirectToAction("", ""));
                            }
                        }
                    }
                }
                ViewBag.Error = "Internal issue, please try again later.";
                return(RedirectToAction("", ""));
            }
            catch (Exception exc)
            {
                ViewBag.Message = "Internar server error, but you'll be redirected anyway." + exc.Message;
                return(RedirectToAction("Index", "Home"));
            }
            //return View();
        }