Esempio n. 1
0
        public async Task <ActionResult> Login(UserAccount account)
        {
            var user = await mongoDatabase.GetUser(account.Email);

            if (user == null)
            {
                // baci ga na view da ne postoji taj user
                ViewBag.ValidationMessage = "Ne postoji user";
                return(View("Login"));
            }
            else
            {
                // check pass
                if (pcrypter.ValidatePassword(account.Password, user.Password))
                {
                    // ok
                    FormsAuthentication.SetAuthCookie("loggedIn", false);
                    Response.SetCookie(new HttpCookie("userEmail", account.Email));
                    return(RedirectToAction("Index", "Dashboard"));
                }
                else
                {
                    // nije dobar pass
                    ViewBag.ValidationMessage = "Nije dobar pass";
                    return(View("Login"));
                }
            }
        }
Esempio n. 2
0
        public async Task <Response <string> > SignIn(User user)
        {
            Response <string> response = new Response <string>();

            // Get the user from the database, check if he actually exists
            var matchingUser = await _userRepository.GetUserByEmail(user.Email);

            if (matchingUser == null)
            {
                return(response.Failed("User does not exist.", "Failed request"));
            }

            // User with this email exists, now check his password
            if (!PasswordCrypter.ValidatePassword(user.Password, matchingUser.Password))
            {
                return(response.Failed("Invalid password.", "Failed request"));
            }

            // Generate token
            // We pass in the matchingUser, since it is a record fron the DB containing all of the fields that will go into claims
            return(response.Success("Successfully signed in.", CreateToken(matchingUser)));
        }