public static ResultModel CreateUser(UserModel user) { ResultModel result = new ResultModel(); //create new user PWSHasher pwHasher = new PWSHasher(); byte[] saltBytes = Encoding.ASCII.GetBytes("ggwp"); HashWithSaltResult hashResult = pwHasher.HashWithGivenSalt(user.password, 64, saltBytes); user.password = hashResult.Digest; using (var db = new ggwpDBEntities()) { Korisnik newKorisnik = new Korisnik(); newKorisnik.username = user.username; newKorisnik.password = user.password; newKorisnik.email = user.email; newKorisnik.ime = user.ime; newKorisnik.dob = user.dob; db.Korisnik.Add(newKorisnik); db.SaveChanges(); user.id = newKorisnik.id; result.SetResults(user, true); } return(result); }
public static Korisnik LoginUser(LoginModel user) { ResultModel result = new ResultModel(); Korisnik kor = null; //login user using (var db = new ggwpDBEntities()) { string dbSalt = "ggwp"; PWSHasher pwHasher = new PWSHasher(); byte[] saltBytes = Encoding.ASCII.GetBytes(dbSalt); HashWithSaltResult hashResult = pwHasher.HashWithGivenSalt(user.password, 64, saltBytes); try { kor = db.Korisnik.Where(x => x.username.Equals(user.username)).SingleOrDefault(); } catch (InvalidOperationException ex) { return(null); } if (hashResult.Digest.Equals(kor.password) && user.username.Equals(kor.username)) { result.SetResults(KorisnikToModel(kor), true); } else { kor = null; } } return(kor); }