Exemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            var salt  = HashingPasswords.GenerateSalt();
            var pw    = HashingPasswords.GenerateHashArgon2(model.Lozinka, salt);
            var uloga = nameof(TipKorisnikaEnum.Korisnik);

            var korisnik = new Models.Korisnik()
            {
                PasswordHash  = pw,
                PasswordSalt  = salt,
                Uloga         = uloga,
                KorisnickoIme = model.KorisnickoIme,
                Ime           = model.Ime,
                Prezime       = model.Prezime,
                Banovan       = false
            };

            _korisnikRepo.Add(korisnik);
            _korisnikRepo.SaveChanges();

            var identity = CreateNewIdentity(model.KorisnickoIme, uloga, korisnik.KorisnikId.ToString());

            var principal = new ClaimsPrincipal(identity);
            await Authentication.Login(HttpContext, principal);

            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PromijeniLozinku(PromijeniLozinkuViewModel model)
        {
            int.TryParse(User?.FindFirst("Id")?.Value, out int korisnikId);

            var korisnik = _korisnikRepo.Get(korisnikId);

            if (korisnik == null)
            {
                return(NotFound());
            }

            var oldPw = HashingPasswords.GenerateHashArgon2(model.TrenutnaLozinka, korisnik.PasswordSalt);

            if (korisnik.PasswordHash != oldPw)
            {
                ModelState.AddModelError("TrenutnaLozinka", "Lozinka je netačna");
                return(View(model));
            }

            var salt = HashingPasswords.GenerateSalt();
            var pw   = HashingPasswords.GenerateHashArgon2(model.NovaLozinka, salt);

            korisnik.PasswordHash = pw;
            korisnik.PasswordSalt = salt;

            _korisnikRepo.Update(korisnik);
            _korisnikRepo.SaveChanges();

            await Authentication.Logout(HttpContext);

            return(RedirectToAction("Login", "Account"));
        }
Exemplo n.º 3
0
        public IHttpActionResult Register([FromBody] RegisterUserVM model)
        {
            using (_userRepo)
            {
                var salt = HashingPasswords.GenerateSalt();
                var pw   = HashingPasswords.GenerateHash(model.Password, salt);

                _userRepo.Add(new User()
                {
                    UserName     = model.UserName,
                    PasswordHash = pw,
                    PasswordSalt = salt
                });
                _userRepo.SaveChanges();

                return(Ok("User registered successfully."));
            }
        }