public static PagingInfo FindAndParsePagingInfo(System.Net.Http.Headers.HttpResponseHeaders responseHeaders)
        {
            IEnumerable <string> paginationHeaderValues;

            responseHeaders.TryGetValues("X-Pagination", out paginationHeaderValues);
            if (paginationHeaderValues.Any())
            {
                return(JsonConvert.DeserializeObject <PagingInfo>(paginationHeaderValues.FirstOrDefault()));
            }
            else
            {
                return(null);
            }
        }
示例#2
0
        public static string GetNextURL(this System.Net.Http.Headers.HttpResponseHeaders headers)
        {
            Regex reg = new Regex(@"<(\S*)>");
            IEnumerable <string> links;

            headers.TryGetValues("Link", out links);

            string Url = links.First().Split(',').FirstOrDefault(s => s.Contains("rel=\"next\""));

            if (Url != null)
            {
                return(Url = reg.Match(Url).Groups[1].Value);
            }
            else
            {
                return(null);
            }
        }
        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();
        }