public ActionResult Login(LoginModel model) { if (!ModelState.IsValid) //Checks if input fields have the correct format { ModelState.AddModelError("LoginMessage", "Specificare nome utente e password"); return(View(model)); //Returns the view with the input values so that the user doesn't have to retype again } NpgsqlConnection con = null; con = Helpers.DbUtils.GetDefaultConnection(); con.Open(); UtenteModel utente = new UtenteModel(); if (utente.login(con, model.nomeUtente, model.password)) { var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, utente.first_name + " " + utente.last_name), new Claim(ClaimTypes.NameIdentifier, model.nomeUtente), }, "ApplicationCookie"); var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; authManager.SignIn(identity); return(Redirect(GetRedirectUrl(model.ReturnUrl))); } con.Close(); ModelState.AddModelError("LoginMessage", "Nome utente o password non validi"); return(View(model)); }
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PolicyRequirement requirement) { var user = new UtenteModel(_httpContextAccessor.HttpContext); if (user.Ruolo == requirement.Ruolo) { context.Succeed(requirement); } return(Task.CompletedTask); }