Beispiel #1
0
        public IActionResult NewUser(Utilizador _model)
        {
            DbContextOptions <PortefolioContext> _options = new DbContextOptions <PortefolioContext>();
            var _context = new PortefolioContext(_options);

            if (ModelState.IsValid)
            {
                //Novo id
                _model.ID = (_context.Utilizadores.Count() + 1).ToString() + "#User";

                //Pass segura com Hash MD5
                _model.PasswordH = Funcionalidades.GetUInt64Hash(MD5.Create(), _model.PasswordH);

                //Senão conseguir gerar a hash
                if (_model.PasswordH == null)
                {
                    return(View(_model));
                }

                _context.Utilizadores.Add(_model);

                _context.SaveChanges();

                return(RedirectToAction("Login", "Account"));
            }
            else
            {
                return(View(_model));
            }
        }
Beispiel #2
0
        public IActionResult Login(IFormCollection col)
        {
            DbContextOptions <PortefolioContext> _options = new DbContextOptions <PortefolioContext>();
            var _context = new PortefolioContext(_options);

            string username = col["usernam"];
            string pass     = col["passw"];

            //Buscar user
            Utilizador _u = _context.Utilizadores.Where(ux => ux.Username == username && ux.IsActive).FirstOrDefault();

            //Verificar user e pass hash
            if (_u != null && Funcionalidades.GetUInt64Hash(MD5.Create(), pass) == _u.PasswordH)
            {
                try
                {
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, _u.Email),
                        new Claim(ClaimTypes.GivenName, _u.Nome)
                    };

                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);

                    HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(claimsIdentity)
                        ).GetAwaiter().GetResult();

                    //return RedirectToAction("Index", "Home");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception information: {0}", ex);
                    return(View(col));
                }
            }
            else
            {
                return(View(col));
            }

            //Se correr bem
            return(RedirectToAction("Index", "Home"));
        }
Beispiel #3
0
        public IActionResult Profile(Utilizador _model, IFormCollection col)
        {
            if (User.Identity.IsAuthenticated)
            {
                DbContextOptions <PortefolioContext> _options = new DbContextOptions <PortefolioContext>();
                var _context = new PortefolioContext(_options);

                //Buscar user
                Utilizador Xuser = _context.Utilizadores.Where(u => u.Email == HttpContext.User.Identity.Name).FirstOrDefault();

                //Alterar dados
                Xuser.Nome     = _model.Nome;
                Xuser.Username = _model.Username;
                Xuser.Email    = _model.Email;

                //Buscar Chave
                string PalavraChave = col["Passwordd"];

                //Se for para mudar a pass
                if (!String.IsNullOrEmpty(PalavraChave))
                {
                    Xuser.PasswordH = Funcionalidades.GetUInt64Hash(MD5.Create(), PalavraChave);
                }

                //Guardar edição
                _context.Utilizadores.Update(Xuser);
                _context.SaveChanges();

                //Notificar
                _toastNotification.AddSuccessToastMessage("Utilizador editado com sucesso!");

                return(View(_model));
            }
            else
            {
                //Não altera
                return(View(_model));
            }
        }