public ActionResult Registration(RegistrationModel registration)
        {
            if (ModelState.IsValid)
            {
                var user = new User()
                {
                    Name             = registration.RegistrationName,
                    Email            = registration.Email,
                    RegistrationDate = DateTime.Now,
                    LastAccessDate   = DateTime.Now
                };
                //var password = registration.UseDefaultPassword
                //    ? ConfigurationManager.AppSettings["UserPassword"]
                //    : registration.NewPassword;
                var password = registration.NewPassword;
                user.PasswordSalt = PasswordHelper.CreateSalt();
                user.Password     = PasswordHelper.GeneratePassword(password, user.PasswordSalt);
                UserRepository.Insert(user);
                Authentication.SaveAuthentication(user.Name, false);

                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                return(View(registration));
            }
        }
示例#2
0
        public string CreateSaltPassowrd(string userName, string password)
        {
            var saltUsername = PasswordHelper.CreateSalt(userName);
            var hashPassword = PasswordHelper.HashPassword(saltUsername, password);

            return(hashPassword);
        }
        /// <summary>
        /// Creates a new User in the database
        /// </summary>
        /// <param name="userDto">User</param>
        /// <returns>Created User</returns>
        public UserDto Create(UserDto userDto)
        {
            ValidateEmail(userDto.Email);
            if (_userRepository.FindByEmail(userDto.Email) == null)
            {
                if (string.IsNullOrEmpty(userDto.Password) || userDto.Password.Length < 4)
                {
                    throw new Exception("The password must have 4 or more characters");
                }
                else
                {
                    string salt = PasswordHelper.CreateSalt();
                    User   user = new User()
                    {
                        Email    = userDto.Email,
                        Salt     = salt,
                        Password = PasswordHelper.HashPassword(userDto.Password, salt),
                        Created  = DateTime.Now,
                        Modified = DateTime.Now
                    };
                    _userRepository.Add(user);
                    _userRepository.DataContext.Commit();

                    userDto.Id = user.Id;
                    return(userDto);
                }
            }
            else
            {
                throw new Exception("User with this email adress already exists.");
            }
        }
        private string getHashedPassword(string password)
        {
            string salt           = passwordHelper.CreateSalt(10);
            string hashedPassword = passwordHelper.GenerateSHA256Hash(password, salt);

            return(string.Concat(salt, hashedPassword));
        }
示例#5
0
        public bool UpdateUserPassword(string email, string oldPassword, string newPassword)
        {
            var user = this.GetUserByEmail(email);

            if (user != null)
            {
                var hashedOldPassword = PasswordHelper.CreatePasswordHash(oldPassword, user.Salt);

                if (user.HashedPassword != hashedOldPassword)
                {
                    return(false);
                }
            }

            var salt = PasswordHelper.CreateSalt(10);
            var hashedNewPassword = PasswordHelper.CreatePasswordHash(newPassword, salt);

            user.Salt           = salt;
            user.HashedPassword = hashedNewPassword;

            this.data.UsersRepository.Update(user);
            this.data.SaveChanges();

            return(true);
        }
示例#6
0
        public HttpResponseMessage CreateUser(UsersCreateModel model)
        {
            bool   existingUser = false;
            bool   isCreated    = false;
            string salt;
            string hashedPassword;

            IEnumerable <User> users = ((IManager)this.usersManager).GetItems() as IEnumerable <User>;

            if (users.Any(u => u.Email == model.Email))
            {
                existingUser = true;
            }

            if (!existingUser)
            {
                User user = this.usersFactory.Create(Guid.NewGuid(), model.Email, model.FirstName, model.LastName, model.IsExternal);

                if (model.IsExternal)
                {
                    user.Image     = imageExtractor.GetImageAsBase64Url(model.AccessToken).Result;
                    salt           = string.Empty;
                    hashedPassword = string.Empty;
                }
                else
                {
                    salt           = PasswordHelper.CreateSalt(10);
                    hashedPassword = PasswordHelper.CreatePasswordHash(model.Password, salt);
                }

                user.Salt           = salt;
                user.HashedPassword = hashedPassword;
                user.UserExternalId = model.UserExternalId;
                user.Age            = model.Age;
                user.PhoneNumber    = model.PhoneNumber;
                user.BloodType      = (BloodType)model.BloodType;

                if (model.PreferredLanguage == "en")
                {
                    user.PreferredLanguage = PreferredLanguage.English;
                }
                else if (model.PreferredLanguage == "bg")
                {
                    user.PreferredLanguage = PreferredLanguage.Bulgarian;
                }

                this.usersManager.CreateItem(user);
                this.usersManager.SaveChanges();
                isCreated = true;
            }

            HttpResponseMessage resp = new HttpResponseMessage();

            resp.Content = new StringContent(JsonConvert.SerializeObject(isCreated));
            resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return(resp);
        }
示例#7
0
文件: User.cs 项目: rummic/Surveys
 public User(RegisterUserDto userData)
 {
     Id       = Guid.NewGuid();
     Name     = userData.Name;
     Email    = userData.Email;
     Salt     = PasswordHelper.CreateSalt();
     Password = PasswordHelper.HashPassword(userData.Password, Salt);
     Role     = Entities.Role.User;
 }
示例#8
0
        public void CreateHashShouldNotReturnPassedPasswordString()
        {
            string userName       = "******";
            string testPassword   = "******";
            string salt           = PasswordHelper.CreateSalt(userName);
            string hashedPassword = PasswordHelper.HashPassword(salt, testPassword);

            Assert.AreNotSame(testPassword, hashedPassword);
        }
示例#9
0
        public void Register(User user, string password)
        {
            var salt           = PasswordHelper.CreateSalt(10);
            var hashedPassword = PasswordHelper.CreatePasswordHash(password, salt);

            user.Salt           = salt;
            user.HashedPassword = hashedPassword;

            this.data.UsersRepository.Add(user);
            this.data.SaveChanges();
        }
示例#10
0
        /// <summary>
        /// Creates the initial user for the testmethods
        /// </summary>
        /// <param name="dbContext">DbContext</param>
        /// <param name="email">Email</param>
        /// <param name="pw">Password</param>
        /// <returns></returns>
        private User CreateInitialUser(DataContext dbContext, string email, string pw)
        {
            User user = new User();

            user.Email    = email;
            user.Salt     = PasswordHelper.CreateSalt();
            user.Password = PasswordHelper.HashPassword(pw, user.Salt);
            dbContext.Users.Add(user);
            dbContext.SaveChanges();
            return(user);
        }
示例#11
0
            public static ClienteSenha NovaSenha(string senha)
            {
                if (string.IsNullOrEmpty(senha))
                {
                    return(new ClienteSenha(null, null));
                }

                var salt = PasswordHelper.CreateSalt();
                var hash = PasswordHelper.HashPassword(senha, salt);

                return(new ClienteSenha(salt, hash));
            }
示例#12
0
        private string HashLoginDetails(string username, string password)
        {
            string result = string.Empty;

            if (username != null && password != null)
            {
                string salt = PasswordHelper.CreateSalt(username);
                result = PasswordHelper.HashPassword(salt, password);
            }

            return(result);
        }
示例#13
0
        public User GetUserByUsernameAndPassword(string username, string password)
        {
            var userSalt         = PasswordHelper.CreateSalt(username);
            var userHashPassword = PasswordHelper.HashPassword(userSalt, password);
            var user             = FilterBuilder()
                                   .Filter(o => o.UserName.ToLower().Equals(username.ToLower()))
                                   .AndAlso(o => o.Password.Equals(userHashPassword))
                                   .AndAlso(o => o.RecordStatus == RecordStatus.Active)
                                   .ExecuteFilter().FirstOrDefault();

            return(user);
        }
        /// <summary>
        /// Resets the password in the db and sends it to the user
        /// </summary>
        /// <param name="email">Email</param>
        /// <param name="sender">Sender</param>
        public void ResetPassword(string email, ISender sender)
        {
            ValidateEmail(email);
            User user = _userRepository.FindByEmail(email);

            if (user != null)
            {
                string pw = PasswordHelper.CreatePassword(6);
                user.Salt     = PasswordHelper.CreateSalt();
                user.Password = PasswordHelper.HashPassword(pw, user.Salt);
                user.Modified = DateTime.Now;
                _userRepository.DataContext.Commit();
                sender.SendPassword(UserMapper.ToDto(user, false), pw);
            }
        }
示例#15
0
        public void Seed(IContext context)
        {
            var salt         = PasswordHelper.CreateSalt("admin");
            var hashPassword = PasswordHelper.HashPassword(salt, "1234");
            var record       = new User
            {
                UserName  = "******",
                CreatedBy = "System",
                Password  = hashPassword
            };

            context.EntitySet <User>().AddOrUpdate(c => new { c.UserName }, record);

            context.SaveChanges();
        }
示例#16
0
        public BaseUser Register(UserDto userDto)
        {
            if (GetUser(userDto) != null)
            {
                throw new FaceAppException("User with this email already registered");
            }
            BaseUser user = ToModel(userDto);

            user.Id           = Guid.NewGuid();
            user.PasswordSalt = PasswordHelper.CreateSalt();
            user.PasswordHash = PasswordHelper.CreatePasswordHash(userDto.Password, user.PasswordSalt);
            Repository.Create(user);
            ApiResponse.UserResponseMessage = "User successfully registered!";
            return(user);
        }
示例#17
0
        public static bool SignUp(RegisterUser reguser)
        {
            bool result = false;

            // Hashing password
            reguser.PasswordSalt = PasswordHelper.CreateSalt();
            reguser.PasswordHash = PasswordHelper.GenerateHash(reguser.Password, reguser.PasswordSalt);

            User usr = Core.RegisterUser(reguser);

            if (usr != null)
            {
                result = true;
            }

            return(result);
        }
示例#18
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var salt = PasswordHelper.CreateSalt();
            var user = new
            {
                Id       = Guid.NewGuid(),
                Name     = "John",
                Email    = "*****@*****.**",
                Role     = "Admin",
                Salt     = salt,
                Password = PasswordHelper.HashPassword("john123", salt)
            };

            var question1 = new Question {
                Id = Guid.NewGuid(), Text = "What is your company size?"
            };
            var question2 = new Question {
                Id = Guid.NewGuid(), Text = "What is your IT team size (if any)?"
            };
            var question3 = new Question {
                Id = Guid.NewGuid(), Text = "What is your growth ambition?"
            };
            var question4 = new Question {
                Id = Guid.NewGuid(), Text = "Do you own/maintain your own IT?"
            };
            var questions = new List <Question>
            {
                question1, question2, question3, question4
            };
            var survey = new Survey {
                Id = Guid.NewGuid(), Name = "Main Survey", CreatorEmail = "*****@*****.**"
            };

            var surveyQuestions = new List <object> {
                new  { Id = Guid.NewGuid(), SurveyId = survey.Id, QuestionId = question1.Id },
                new  { Id = Guid.NewGuid(), SurveyId = survey.Id, QuestionId = question2.Id },
                new  { Id = Guid.NewGuid(), SurveyId = survey.Id, QuestionId = question3.Id },
                new  { Id = Guid.NewGuid(), SurveyId = survey.Id, QuestionId = question4.Id }
            };

            modelBuilder.Entity <User>().HasData(user);
            modelBuilder.Entity <Question>().HasData(questions);
            modelBuilder.Entity <Survey>().HasMany(x => x.SurveyQuestions).WithOne(x => x.Survey);
            modelBuilder.Entity <Survey>().HasData(survey);
            modelBuilder.Entity <SurveyQuestion>().HasData(surveyQuestions);
        }
示例#19
0
        public HttpResponseMessage ChangePassword(ChangePasswordModel model)
        {
            HttpResponseMessage resp = new HttpResponseMessage();

            IEnumerable <User>  users  = this.usersManager.GetItems() as IEnumerable <User>;
            IEnumerable <Claim> claims = (HttpContext.Current.User as ClaimsPrincipal).Claims;

            string userEmail = claims.Where(c => c.Type == ClaimTypes.Email).Select(c => c.Value).FirstOrDefault();

            if (!string.IsNullOrEmpty(userEmail))
            {
                User user = users.Where(u => u.Email == userEmail).FirstOrDefault();

                string hashedOldPassword = PasswordHelper.CreatePasswordHash(model.OldPassword, user.Salt);

                if (user.HashedPassword == hashedOldPassword)
                {
                    string salt = PasswordHelper.CreateSalt(10);
                    string hashedNewPassword = PasswordHelper.CreatePasswordHash(model.NewPassword, salt);

                    user.Salt           = salt;
                    user.HashedPassword = hashedNewPassword;

                    this.usersManager.UpdateItem(user);
                    this.usersManager.SaveChanges();

                    resp.Content = new StringContent(JsonConvert.SerializeObject(new ChangeGeneralResponseViewModel()
                    {
                        IsSuccessful = true
                    }));
                    resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                }
                else
                {
                    resp.Content = new StringContent(JsonConvert.SerializeObject(new ChangeGeneralResponseViewModel()
                    {
                        IsSuccessful = false, State = "incorrect_password"
                    }));
                    resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                }
            }

            return(resp);
        }
示例#20
0
        public IActionResult InsertStudent(StudentApbd req)
        {
            var db   = new s17057Context();
            var stud = new StudentApbd();

            stud.SetProperty("FirstName", req.FirstName);
            stud.SetProperty("IndexNumber", req.IndexNumber);
            stud.SetProperty("LastName", req.LastName);
            stud.SetProperty("BirthDate", req.BirthDate);
            stud.SetProperty("IdEnrollment", req.IdEnrollment);

            var salt = PasswordHelper.CreateSalt();

            stud.SetProperty("Salt", salt);
            stud.SetProperty("Pswd", PasswordHelper.GenerateSaltedHash(req.Pswd, salt));
            db.StudentApbd.Add(req);
            db.SaveChanges();
            return(Ok("Insert zakończony pomyślnie"));
        }
示例#21
0
        public HttpResponseMessage ResetPassword(UsersResetPasswordModel model)
        {
            HttpResponseMessage resp = new HttpResponseMessage();

            IEnumerable <User> users = this.usersManager.GetItems() as IEnumerable <User>;

            User user = users.Where(u => u.Email == model.Email).FirstOrDefault();

            if (user != null)
            {
                if (user.ResetCode == model.ResetCode)
                {
                    string salt = PasswordHelper.CreateSalt(10);
                    string hashedNewPassword = PasswordHelper.CreatePasswordHash(model.NewPassword, salt);

                    user.Salt           = salt;
                    user.HashedPassword = hashedNewPassword;
                    user.ResetCode      = null;

                    this.usersManager.UpdateItem(user);
                    this.usersManager.SaveChanges();

                    resp.Content = new StringContent(JsonConvert.SerializeObject(new ChangeGeneralResponseViewModel()
                    {
                        IsSuccessful = true
                    }));
                    resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                }
                else
                {
                    resp.Content = new StringContent(JsonConvert.SerializeObject(new ChangeGeneralResponseViewModel()
                    {
                        IsSuccessful = false, State = "incorrect_reset_code"
                    }));
                    resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                }
            }


            return(resp);
        }
示例#22
0
 public static bool ResetPassword(LoginUserValidation user)
 {
     user.PasswordSalt = PasswordHelper.CreateSalt();
     user.PasswordHash = PasswordHelper.GenerateHash(user.PasswordHash, user.PasswordSalt);
     return(Core.ResetPassword(user));
 }
示例#23
0
        public void CreateSalt()
        {
            string salt = PasswordHelper.CreateSalt();

            Assert.IsTrue(!string.IsNullOrEmpty(salt));
        }