Esempio n. 1
0
        private string SignInStudent(LoginLogicModel model)
        {
            using (var context = new AASDBContext())
            {
                var student = context.Students.SingleOrDefault(a => a.Email == model.Username);
                if (student == null)
                {
                    throw new LoginErrorException();
                }

                if (string.IsNullOrWhiteSpace(student.Salt))
                {
                    student.Salt     = PasswordHashProvider.GenerateSalt();
                    student.Password = PasswordHashProvider.ComputePasswordHash(student.Password.Trim(), student.Salt);
                    context.SaveChanges();
                }

                var hash = PasswordHashProvider.ComputePasswordHash(model.Password, student.Salt);

                if (student.Password != hash)
                {
                    throw new LoginErrorException();
                }

                return(ComposeToken(model.Username, UserType.StudentUserType));
            }
        }
Esempio n. 2
0
        private string SignInAdmin(LoginLogicModel model)
        {
            using (var context = new AASDBContext())
            {
                var admin = context.Admins.SingleOrDefault(a => a.Username == model.Username);
                if (admin == null)
                {
                    throw new LoginErrorException();
                }

                if (string.IsNullOrWhiteSpace(admin.Salt))
                {
                    admin.Salt     = PasswordHashProvider.GenerateSalt();
                    admin.Password = PasswordHashProvider.ComputePasswordHash(admin.Password.Trim(), admin.Salt);
                    context.SaveChanges();
                }

                var hash = PasswordHashProvider.ComputePasswordHash(model.Password, admin.Salt);

                if (admin.Password != hash)
                {
                    throw new LoginErrorException();
                }

                return(ComposeToken(model.Username, UserType.AdminUserType));
            }
        }
        public IHttpActionResult TestSalt()
        {
            var hash = PasswordHashProvider.GenerateSalt();

            return(Json(new
            {
                Hash = hash,
                Length = hash.Length
            }));
        }