示例#1
0
        private FireSys.Entities.AspNetUser TryToLogin(LoginViewModel model)
        {
            // Get all users from DB
            IEnumerable <FireSys.Entities.AspNetUser> users = UserManager.Find(u => u.UserName == model.Username);

            if (users == null || users.Count() == 0)
            {
                model.AlertMessage = "User not found! Try with different email and password!";
                return(null);
            }

            // Get specific user
            FireSys.Entities.AspNetUser user = users.FirstOrDefault(x => x.UserName == model.Username);


            // If user don't exist return null
            if (user == null)
            {
                model.AlertMessage = "User not found! Try with different email and password!";
                return(null);
            }


            // Verify users password and return user if password matches, otherwise retun null
            if (PasswordHasher.VerifyHashedPassword(user.PasswordHash, model.Password) == PasswordVerificationResult.Success)
            {
                return(user);
            }
            else
            {
                model.AlertMessage = "Password incorect. Try again.";
                return(null);
            }
        }
示例#2
0
        public ActionResult SaveUserInfo(UserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Edit", model));
            }

            UserManager userManager = new UserManager();

            FireSys.Entities.AspNetUser updateUser = userManager.Find(u => u.UserName == User.Identity.Name).FirstOrDefault();
            updateUser.FirstName = model.UserInfo.FirstName;
            updateUser.LastName  = model.UserInfo.LastName;
            userManager.Update(updateUser);


            return(View("Index"));
        }
示例#3
0
        public bool SignIn(LoginViewModel userModel)
        {
            // Get user
            FireSys.Entities.AspNetUser user = this.TryToLogin(userModel);
            if (user == null)
            {
                return(false);
            }

            List <Claim> userClaims = new List <Claim>();

            userClaims.Add(new Claim(ClaimTypes.Name, user.UserName));
            //userClaims.Add(new Claim(ClaimTypes.NameIdentifier, user.UserId));

            //// Get roles
            IEnumerable <UserRole> roles = null;// UserRoleManager.Find(r => r.UserId == user.UserId);

            //roles = roles == null ? roles : roles.Where(x => x.UserId == user.UserId).ToList();

            // Uncomment this when roles are provided
            userClaims.Add(new Claim(ClaimTypes.Role, "user"));
            if (roles != null)
            {
                roles.ToList().ForEach(r => userClaims.Add(new Claim(ClaimTypes.Role, r.Role.Name)));
            }

            ClaimsIdentity userClaimIdentity = new ClaimsIdentity(userClaims, DefaultAuthenticationTypes.ApplicationCookie);

            this.AuthenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = userModel.RememberMe,
                ExpiresUtc   = DateTime.UtcNow.AddDays(1)
            }, userClaimIdentity);

            return(true);
        }