Esempio n. 1
0
        public ActionResult Login([FromUri] string error = null)
        {
            var model = new UsersDetailsViewModel();

            if (!string.IsNullOrEmpty(error))
            {
                if (error.ToLower() == "unauthorized")
                {
                    ModelState.AddModelError("", "You must login in order to access that page");
                }
            }

            ViewBag.Title = "Login";

            return View(model);
        }
Esempio n. 2
0
        public ActionResult Login(UsersDetailsViewModel model)
        {
            try
            {
                var existingLogin = webClient.ExecuteLoginPost<object>(new Models.ApiRequest()
                {
                    EndPoint = string.Format("logins"),
                    Request = new UserDto()
                    {
                        Username = model.Username,
                        Password = model.Password
                    }
                }) as JObject;

                if (existingLogin != null)
                {
                    var parsedLogin = existingLogin.ToObject<LoginDto>();
                    Session[Constants.Headers.AuthTokenHeader] = parsedLogin.AuthToken;

                    var user = webClient.ExecuteGet<UserDto>(new Models.ApiRequest() { EndPoint = string.Format("users/details/{0}", model.Username) });
                    if (user != null && user.IsAdmin)
                    {
                        Session[Constants.Headers.AdminTokenHeader] = parsedLogin.AuthToken;
                        Session[Constants.Headers.AuthTokenHeader] = null;
                    }

                    Session["LoginId"] = parsedLogin.Id;
                    Session["Username"] = model.Username;
                }
                else
                {
                    ModelState.AddModelError("", "Username or Password is wrong.");
                    return View();
                }

                return RedirectToAction("Index", "Home");
            }
            catch
            {
                ModelState.AddModelError("", "Oops. Something happened.");
                return View();
            }
        }
Esempio n. 3
0
        // GET: Users/Details/5
        public ActionResult Details([FromUri] string username)
        {
            var user = webClient.ExecuteGet<UserDto>(new Models.ApiRequest() { EndPoint = $"users/details/{username}" });

            var model = new UsersDetailsViewModel();

            if (user != null)
            {
                model.Id = user.Id;
                model.FullName = user.FullName;
                model.Username = user.Username;
                model.IsAdmin = user.IsAdmin;
                model.Rating = user.Rating;

                ViewBag.Title = model.Username;
            }

            return View(model);
        }
Esempio n. 4
0
        public ActionResult Register(UsersDetailsViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Session[Constants.Headers.AuthTokenHeader] = "registertoken";

                    var existingLogin = webClient.ExecutePost<object>(new Models.ApiRequest()
                    {
                        EndPoint = string.Format("users"),
                        Request = new UserDto()
                        {
                            FullName = model.FullName,
                            Username = model.Username,
                            Password = model.Password,
                            IsAdmin = false
                        }
                    }) as JObject;


                    if (existingLogin != null)
                    {
                        var parsedLogin = existingLogin.ToObject<LoginDto>();
                        Session[Constants.Headers.AuthTokenHeader] = parsedLogin.AuthToken;
                        Session["LoginId"] = parsedLogin.Id;
                        Session["Username"] = model.Username;
                    }

                    return RedirectToAction("Index", "Home");
                }
                catch (HttpException ex)
                {
                    if (ex.GetHttpCode() != (int)HttpStatusCode.BadRequest)
                    {
                        throw ex;
                    }

                    ModelState.AddModelError("", "This username is already taken! Please choose another one.");
                    Session[Constants.Headers.AuthTokenHeader] = null;
                    return View();
                }
                catch
                {
                    Session[Constants.Headers.AuthTokenHeader] = null;
                    return View();
                }
            }

            return View();
        }