Esempio n. 1
0
        public IHttpResponse DoLogin(IHttpRequest request)
        {
            var userName = request.FormData["username"].ToString().Trim();
            var password = request.FormData["password"].ToString();

            var hashedPassword = hashService.Hash(password);

            var user = Db.Users.FirstOrDefault(x => x.Username == userName && x.Password == hashedPassword);

            if (user == null)
            {
                return(BadRequestError("Invalid username or password."));
            }

            var cookieContent = UserCookieService.GetUserCookie(user.Username);

            var response = new RedirectResult("/");
            var cookie   = new HttpCookie(".auth-cakes", cookieContent, 7)
            {
                HttpOnly = true
            };

            response.Cookies.Add(cookie);
            return(response);
        }
Esempio n. 2
0
        public IHttpResponse Login(DoLoginInputModel model)
        {
            if (User.IsLoggedIn)
            {
                return(Redirect("/"));
            }

            var hashedPassword = _hashService.Hash(model.Password);

            var user = Db.Users.FirstOrDefault(x =>
                                               x.Username == model.Username.Trim() &&
                                               x.Password == hashedPassword);

            if (user == null)
            {
                return(BadRequestErrorWithView("Invalid username or password."));
            }

            var mvcUser = new MvcUserInfo
            {
                Username = user.Username,
                Role     = user.Role.ToString(),
                Info     = user.Email,
            };
            var cookieContent = UserCookieService.GetUserCookie(mvcUser);

            var cookie = new HttpCookie(".auth-cakes", cookieContent, 7)
            {
                HttpOnly = true
            };

            Response.Cookies.Add(cookie);

            return(Redirect("/"));
        }
Esempio n. 3
0
        public IHttpResponse DoLogin(LoginViewModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Username) || string.IsNullOrWhiteSpace(model.Password) ||
                string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Password))
            {
                var errorMessage = "Invalid username or password.";
                return(BadRequestError(errorMessage));
            }

            var hashedPassword = HashService.Hash(model.Password);

            using (Db)
            {
                var user = Db.Users.FirstOrDefault(u => u.Username.Equals(model.Username));

                bool isEqual = user != null && user.Username.Equals(model.Username);

                if (!Db.Users.Any(u => u.Password.Equals(hashedPassword)) || !isEqual)
                {
                    var errorMessage = "Invalid username or password.";
                    return(BadRequestError(errorMessage));
                }

                ViewData["greeting"] = user.Name;
            }


            Request.Session.AddParameter(".auth_cake", model.Username);

            Request.Session.AddParameter(ShoppingCartViewModel.SessionKey, new ShoppingCartViewModel());

            ViewData["authenticated"]    = "bloc";
            ViewData["cart"]             = "bloc";
            ViewData["notAuthenticated"] = "none";
            ViewData["title"]            = "Home";
            ViewData["searchTerm"]       = null;

            var cookieContent = UserCookieService.GetUserCookie(model.Username);

            Response.Cookies.Add(new HttpCookie(".auth_cake", $"{cookieContent}; {GlobalConstants.HttpOnly}", 7));

            return(View("/"));
        }
Esempio n. 4
0
        public IHttpResponse LoginPost(IHttpRequest request)
        {
            string username = request.FormData["username"].ToString().Trim();
            string password = request.FormData["password"].ToString();

            string hashedPassword = hashService.Hash(password);

            User user = Db.Users.FirstOrDefault(u => u.Username == username && u.Password == hashedPassword);

            if (user == null)
            {
                return(BadRequestError("Invalid username or password"));
            }

            IHttpResponse response = new RedirectResult("/");

            string cookie = UserCookieService.GetUserCookie(username);

            response.Cookies.Add(new HttpCookie(AuthCookieHeaderName, cookie, 7));

            return(response);
        }
Esempio n. 5
0
        public IHttpResponse PostLogin(IHttpRequest request)
        {
            var username = request.FormData["username"].ToString();
            var password = request.FormData["password"].ToString();

            var hashService   = new HashService();
            var cookieService = new UserCookieService();

            var hashedPassword = hashService.Hash(password);

            var user = this.Context.Users.FirstOrDefault(u => u.Username == username && u.HashedPassword == hashedPassword);

            if (user == null)
            {
                return(new RedirectResult("/login"));
            }

            request.Session.AddParameter("username", username);
            var userCookieValue = cookieService.GetUserCookie(username);

            request.Cookies.Add(new HttpCookie("IRunes_auth", userCookieValue));
            return(new RedirectResult("home/index"));
        }
Esempio n. 6
0
        public IHttpResponse Login(LoginViewModel model)
        {
            if (!_usersService.UserIsAuthenticated(model, Db))
            {
                var errorMessage = "Invalid username or password.";
                return(View("error", new ErrorViewModel(errorMessage)));
            }

            var userViewModel = _usersService.Profile(model.Username, Db);

            var mvcUser = new MvcUserInfo {
                Username = userViewModel.Username, Role = userViewModel.Role, Info = userViewModel.Email
            };

            var cookieContent = UserCookieService.GetUserCookie(mvcUser);

            Request.Session.AddParameter(".auth_cake", cookieContent);

            Response.Cookies.Add(new HttpCookie(".auth_cake", $"{cookieContent}; {GlobalConstants.HttpOnly}", 7));


            return(View("/home/loggedInUser", userViewModel));
        }