Example #1
0
        public async Task <IActionResult> Registar([FromForm] Utilizador ut)
        {
            try
            {
                Utilizador u = _context.Utilizador.Where(c => c.Email == ut.Email).FirstOrDefault();
                if (u != null)
                {
                    return(Redirect("/?email=" + ut.Email));
                }
                ut.Id       = 0;
                ut.Password = Myhelper.HashPassword(ut.Password);
                System.Diagnostics.Debug.WriteLine(ut.Password);
                _context.Utilizador.Add(ut);
                _context.SaveChanges();
                List <Claim> claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, ut.Id.ToString()),
                };
                ClaimsIdentity  cIdentity = new ClaimsIdentity(claims, "login");
                ClaimsPrincipal principal = new ClaimsPrincipal(cIdentity);
                await HttpContext.SignInAsync(principal);

                //Request.HttpContext.
                return(Redirect("/Home/Tutorial/"));
            }
            catch
            {
                return(Redirect("/?email=" + ut.Email));
            }
        }
Example #2
0
        public async Task <IActionResult> PostAsync([FromForm] string email, [FromForm] string password)
        {
            if (email == null || password == null)
            {
                return(BadRequest(new
                {
                    emailMissing = email == null,
                    passwordMissing = password == null
                }));
            }
            Utilizador utilizador = new Utilizador()
            {
                Email    = email,
                Password = Myhelper.HashPassword(password)
            };

            try
            {
                utilizador = _context.Utilizador.Where(c => c.Email == utilizador.Email && c.Password == utilizador.Password).Single();
                List <Claim> claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, utilizador.Id.ToString()),
                };
                ClaimsIdentity  cIdentity = new ClaimsIdentity(claims, "login");
                ClaimsPrincipal principal = new ClaimsPrincipal(cIdentity);
                await HttpContext.SignInAsync(principal);

                //Request.HttpContext.
                return(Redirect("/Home/Cozinhar/"));
            }
            catch
            {
                return(Redirect("/?email=" + email));
            }
        }