public async Task <IActionResult> UserLogin([Bind] UserLoginViewModel user)
        {
            if (ModelState.IsValid)
            {
                string LoginStatus = userlogic.ValidateLogin(UserLoginViewModel.ConvertViewModelToModel(user));

                if (LoginStatus == "Success")
                {
                    var all = _user.GetUsers();
                    UVM = new List <UserViewModel>();
                    //If the ID isn't equil to Null-value, the if-statement is executed.
                    if (user.Email != null)
                    {
                        //Here it count with int 'i' and it keeps counting 'til the max value of all is counted.
                        for (int i = 0; i < all.Count; i++)
                        {
                            //When ID is equil to all; the program will 'copy' all values of Vehicleviewmodel and add it to VVM.
                            if (user.Email == all[i].Email)
                            {
                                UVM.Add(new UserViewModel
                                {
                                    ID          = all[i].ID,
                                    Firstname   = all[i].Firstname,
                                    Lastname    = all[i].Lastname,
                                    Email       = all[i].Email,
                                    Adres       = all[i].Adres,
                                    Housenumber = all[i].Housenumber,
                                    Password    = all[i].Password,
                                    Postalcode  = all[i].Postalcode,
                                    Role        = all[i].Role,
                                });

                                var claims = new List <Claim>
                                {
                                    new Claim(ClaimTypes.Email, user.Email),
                                    new Claim(ClaimTypes.Role, all[i].Role),
                                };
                                ClaimsIdentity  userIdentity = new ClaimsIdentity(claims, "login");
                                ClaimsPrincipal principal    = new ClaimsPrincipal(userIdentity);

                                await HttpContext.SignInAsync(principal);

                                TempData["LoggedIn"] = "You have logged in with your personal account.";
                                return(RedirectToAction("Index", "Home"));
                            }
                        }
                    }
                }
                else
                {
                    TempData["UserLoginFailed"] = "Login Failed.Please enter correct credentials";
                    return(View());
                }
            }
            return(View());
        }
Ejemplo n.º 2
0
        /* INDEX ------------------------------------------- INDEX ------------------------------------------------------ INDEX --------------------------------------------------- INDEX ------------------------------------ INDEX*/

        public IActionResult Index()
        {
            var all = _coll.GetUsers();

            userviews = new List <UserViewModel>();

            foreach (var user in all)
            {
                userviews.Add(UserViewModel.ConvertModelToUserViewModel(user));
            }

            return(View(userviews));
        }