public ActionResult Login(LoginViewModel loginmodel) { if (!ModelState.IsValid) //Checks if input fields have the correct format { return View(loginmodel); //Returns the view with the input values so that the user doesn't have to retype again } if (_userService.IfUserExistWithThisUserAndPass(loginmodel.Email,loginmodel.Password)) { var logedinuser = _userService.GetAll().Find(u => u.Email == loginmodel.Email); var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, logedinuser.FirstName), new Claim(ClaimTypes.Email, loginmodel.Email), //new Claim(ClaimTypes.Country, "XtiansCountrie"), //new Claim(ClaimTypes.Role, "RoleAdmin"), //new Claim(ClaimTypes.Role, "Admin") }, "ApplicationCookie"); foreach (var role in logedinuser.Roles) { identity.AddClaim(new Claim(ClaimTypes.Role, role.RoleName)); } var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; authManager.SignIn(identity); return Redirect(GetRedirectUrl(loginmodel.ReturnUrl)); } ModelState.AddModelError("", "Invalid email or password"); return View(loginmodel); }
public ActionResult Login(string returnUrl) { var model = new LoginViewModel { ReturnUrl = returnUrl }; return View(model); }