/// <summary>
        /// Retrieve the info of a specific player
        /// in the form of a BSonDocument
        /// (Used for logged in player)
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static BsonDocument getSpecificPlayer(string username, string password)
        {
            var player = getAllUsersCollection().FindOne(Query.EQ("pl_username", username));

            return(player["pl_password"].
                   Equals(PasswordHashAndSalt.GenerateSaltedHash(PasswordHashAndSalt.getBytes(password), player["pl_passwordSalt"].AsByteArray))
                ? player: null);
        }
        public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel forgotPassword)
        {
            var recaptcha          = new ReCaptcha();
            var responseFromServer = recaptcha.OnActionExecuting();

            if (responseFromServer.StartsWith("true", StringComparison.Ordinal))
            {
                if (forgotPassword != null && ModelState.IsValid)
                {
                    var userByUsername = await DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                        { "Account.UserName", forgotPassword.UserName }
                    }).ToListAsync();

                    if (userByUsername.Count > 0)
                    {
                        if (userByUsername[0].Account.Email.Equals(forgotPassword.Email))
                        {
                            var password              = CreatePassword();
                            var passwordEncryption    = new PasswordHashAndSalt();
                            var tempEncryptedPassword = passwordEncryption.getHashedPassword(password);
                            userByUsername[0].Account.TempPassword = tempEncryptedPassword;
                            await DatabaseContext.RegisteredUsers.ReplaceOneAsync(r => r.Account.UserName == userByUsername[0].Account.UserName, userByUsername[0]);

                            using (var mail = new MailMessage())
                            {
                                mail.To.Add(forgotPassword.Email);
                                mail.Subject = "Royal Holloway LETS Password Recovery";
                                mail.Body    = "<p>Hello " + userByUsername[0].About.FirstName + ",</p><h3>Forgotten your password?</h3><p>We got a request to reset your Royal Holloway LETS account's password.<br/>You use the below code in bold to login to your account.<br/><b>Please change your password to something memorable when you have logged in.</b></p><h2>" + password + "</h2><p>All the best,<br/>Royal Holloway LETS</p>";
                                SendEmail(mail);
                                ModelState.AddModelError("Success", "Please check you email, We have sent you your recovery password to your account.");
                                forgotPassword.UserName = null;
                                forgotPassword.Email    = null;
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("Email", "Sorry, The Email you provided is not associated with the username you entered.");
                            return(View(forgotPassword));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "Sorry, We didn't find any account associated with this username in our system.");
                    }
                }
            }
            else
            {
                ModelState.AddModelError("ReCaptcha", "Incorrect CAPTCHA entered.");
                return(View(forgotPassword));
            }
            return(View());
        }
示例#3
0
        public IActionResult SaveStudent(StudentViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("StudentRegistration", ListsInput(model)));
            }

            var contact = new ContactInfo()
            {
                Email   = model.Email,
                Phone   = model.PhoneNummber,
                Address = model.Address
            };

            db.Contact.Add(contact);
            db.SaveChanges();

            var profileInfo = new ProfileInfo()
            {
                Username     = model.Username,
                PasswordSalt = PasswordHashAndSalt.GenerateSalt()
            };

            profileInfo.PasswordHash = PasswordHashAndSalt.GenerateHash(profileInfo.PasswordSalt, model.Password);
            db.ProfileInfo.Add(profileInfo);
            db.SaveChanges();

            var newStudent = new Student()
            {
                FName         = model.FName,
                LName         = model.LName,
                DateOfBirth   = model.DateOfBirth,
                DateAdded     = DateTime.Today,
                ContactId     = contact.Id,
                CityId        = model.CityId,
                StudentTypeId = model.StudentTypeId,
                StatusId      = 1,
                GenderId      = model.GenderId,
                ProfileInfoId = profileInfo.Id
            };

            db.Student.Add(newStudent);
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
示例#4
0
        public IActionResult UpdateStudent(StudentViewModel model)
        {
            //Treba validacija za Email i telefon, za addrtesu ne treba jer neki useri mogu da dijele adresu


            var editedStudent = db.Student.FirstOrDefault(x => x.Id == model.StudentId);
            var contactInfo   = db.Contact.FirstOrDefault(x => x.Id == editedStudent.ContactId);
            var profileInfo   = db.ProfileInfo.FirstOrDefault(x => x.Id == editedStudent.ProfileInfoId);

            contactInfo.Email   = model.Email;
            contactInfo.Phone   = model.PhoneNummber;
            contactInfo.Address = model.Address;


            db.Contact.Update(contactInfo);
            db.SaveChanges();

            profileInfo.Username = model.Username;
            if (!String.IsNullOrEmpty(model.Password))
            {
                profileInfo.PasswordSalt = PasswordHashAndSalt.GenerateSalt();
                profileInfo.PasswordHash = PasswordHashAndSalt.GenerateHash(profileInfo.PasswordSalt, model.Password);
            }

            db.Update(profileInfo);
            db.SaveChanges();

            editedStudent.FName          = model.FName;       //readonly
            editedStudent.LName          = model.LName;       //readonly
            editedStudent.DateOfBirth    = model.DateOfBirth; //readonly
            editedStudent.DateAdded      = model.DateAdded;   //readonly
            editedStudent.ContactId      = contactInfo.Id;
            editedStudent.ProfileInfoId  = profileInfo.Id;
            editedStudent.CityId         = model.CityId;
            editedStudent.StudentTypeId  = model.StudentTypeId;
            editedStudent.ProfilePicture = model.ProfilePicture;//ako su isti biti ko vec nemjenjaj nista ako nisu onda mjenjaj treba dodat isto treba kompresovat sliku



            db.Update(editedStudent);
            db.SaveChanges();

            //Redirekcija se treba stavit
            return(RedirectToAction("Index", "Home"));
        }
        public IActionResult UpdatedAdministrator(AdministratorVM obj)
        {
            var contactInfo = new ContactInfo()
            {
                Address = obj.Address,
                Email   = obj.Email,
                Phone   = obj.Phone
            };

            db.Contact.Update(contactInfo);
            db.SaveChanges();

            var profileInfo = new ProfileInfo()
            {
                Username = obj.Username,
            };

            if (!String.IsNullOrEmpty(obj.Password))
            {
                profileInfo.PasswordSalt = PasswordHashAndSalt.GenerateSalt();
                profileInfo.PasswordHash = PasswordHashAndSalt.GenerateHash(profileInfo.PasswordSalt, obj.Password);
            }

            db.ProfileInfo.Update(profileInfo);
            db.SaveChanges();


            var editedAdministrator = new Administrator()
            {
                FName = obj.FirstName,
                LName = obj.LastName,
                AdministrastorRoleId = obj.AdministrastorRoleId,
                CityId        = obj.CityId,
                ProfileInfoId = profileInfo.Id,
                ContactInfoId = contactInfo.Id
            };

            db.Administrator.Update(editedAdministrator);
            db.SaveChanges();


            return(RedirectToAction());
        }
        /// <summary>
        /// Save the information of a new player
        /// </summary>
        /// <param name="username">Their username</param>
        /// <param name="password">Their password (Hashed and salted in method)</param>
        public static void addNewPlayer(string username, string password)
        {
            byte[] salt = PasswordHashAndSalt.CreateSalt();

            var player = new BsonDocument
            {
                { "_id", getUnusedID() },
                { "pl_username", username },
                { "pl_usernameLower", username.ToLower() },
                { "pl_password", PasswordHashAndSalt.GenerateSaltedHash(PasswordHashAndSalt.getBytes(password), salt) },
                { "pl_passwordSalt", salt },
                { "pl_joinDate", DateTime.Today.ToString("dd.MM.yyyy") },
                { "pl_records", new BsonArray {
                      createPlayerRecord(1, "Registered for the game.")
                  } }
            };

            getAllUsersCollection().Insert(player);
        }
        public async Task <ActionResult> ChangePassword(RegisterUserViewModel registeredUser)
        {
            if (User != null)
            {
                var username       = User.Identity.Name;
                var userByUsername = await DatabaseContext.RegisteredUsers.Find(new BsonDocument
                {
                    { "Account.UserName", username }
                }).ToListAsync();

                var passwordEncryption = new PasswordHashAndSalt();
                var oldPassword        = passwordEncryption.getHashedPassword(registeredUser.Account.OldPassword);
                var newPassword        = passwordEncryption.getHashedPassword(registeredUser.Account.NewPassword);
                var confirmNewPassword = passwordEncryption.getHashedPassword(registeredUser.Account.ConfirmNewPassword);

                if (userByUsername != null && userByUsername.Count > 0 && newPassword.Equals(confirmNewPassword))
                {
                    if (userByUsername[0].Account.Password.Equals(oldPassword) ||
                        (!string.IsNullOrEmpty(userByUsername[0].Account.TempPassword) &&
                         userByUsername[0].Account.TempPassword.Equals(oldPassword)))
                    {
                        userByUsername[0].Account.Password     = newPassword;
                        userByUsername[0].Account.TempPassword = null;
                        await DatabaseContext.RegisteredUsers.ReplaceOneAsync(r => r.Account.UserName == userByUsername[0].Account.UserName, userByUsername[0]);

                        TempData.Add("PasswordChanged", "Your Password was changed successfully.");
                    }
                    else
                    {
                        TempData.Add("PasswordNotChanged", "There was an error in changing you password. Please try again.");
                    }
                }
            }
            else
            {
                TempData.Add("PasswordNotChanged", "There was an error in changing you password. Please try again.");
            }
            return(RedirectToAction("UserProfile", "Account"));
        }
        public IActionResult SaveAdministrator(AdministratorVM obj)
        {
            var contactInfo = new ContactInfo()
            {
                Address = obj.Address,
                Email   = obj.Email,
                Phone   = obj.Phone
            };

            db.Contact.Add(contactInfo);
            db.SaveChanges();

            var profileInfo = new ProfileInfo()
            {
                Username     = obj.Username,
                PasswordSalt = PasswordHashAndSalt.GenerateSalt()
            };

            profileInfo.PasswordHash = PasswordHashAndSalt.GenerateHash(profileInfo.PasswordSalt, obj.Password);
            db.ProfileInfo.Add(profileInfo);
            db.SaveChanges();


            var newAdministrator = new Administrator()
            {
                FName = obj.FirstName,
                LName = obj.LastName,
                AdministrastorRoleId = obj.AdministrastorRoleId,
                CityId        = obj.CityId,
                DateAdded     = DateTime.Now.ToUniversalTime(),
                ProfileInfoId = profileInfo.Id,
                ContactInfoId = contactInfo.Id
            };

            db.Administrator.Add(newAdministrator);
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Login(LoginViewModel loginUser)
        {
            if (loginUser != null && ModelState.IsValid)
            {
                var userByUsername = await DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                    { "Account.UserName", loginUser.UserName }
                }).ToListAsync();

                var passowordEncryption = new PasswordHashAndSalt();
                loginUser.Password = passowordEncryption.getHashedPassword(loginUser.Password);

                if (userByUsername.Count > 0)
                {
                    if (userByUsername[0].Account.UserName.Equals(loginUser.UserName) && (userByUsername[0].Account.Password.Equals(loginUser.Password) || (!string.IsNullOrEmpty(userByUsername[0].Account.TempPassword) && userByUsername[0].Account.TempPassword.Equals(loginUser.Password))))
                    {
                        var userAuthentication = new UserAuthentication();
                        var identity           = userAuthentication.AuthenticateUser(userByUsername[0].Account.UserName);
                        HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties {
                            IsPersistent = false, ExpiresUtc = DateTime.UtcNow + TimeSpan.FromMinutes(15)
                        }, identity);
                        return(RedirectToAction("UserProfile", "Account"));
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "Please make sure you entered the correct username.");
                        ModelState.AddModelError("Password", "Please make sure you entered the correct password.");
                        View();
                    }
                }
                else
                {
                    ModelState.AddModelError("UserName", "Please make sure you entered the correct username.");
                    ModelState.AddModelError("Password", "Please make sure you entered the correct password.");
                    return(View());
                }
            }
            return(View());
        }
        public ActionResult Register(RegisterUserViewModel registerUser)
        {
            var recaptcha          = new ReCaptcha();
            var responseFromServer = recaptcha.OnActionExecuting();

            if (responseFromServer.StartsWith("true", StringComparison.Ordinal))
            {
                if (registerUser != null && ModelState.IsValid)
                {
                    var userByUsername = DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                        { "Account.UserName", registerUser.Account.UserName }
                    }).ToList();

                    var userByEmail = DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                        { "Account.Email", registerUser.Account.Email }
                    }).ToList();

                    if (userByUsername.Count == 0)
                    {
                        if (userByEmail.Count == 0)
                        {
                            var passwordEncryption = new PasswordHashAndSalt();
                            registerUser.Id = Guid.NewGuid().ToString();
                            registerUser.Account.Password        = passwordEncryption.getHashedPassword(registerUser.Account.Password);
                            registerUser.Account.ConfirmPassword = passwordEncryption.getHashedPassword(registerUser.Account.ConfirmPassword);
                            registerUser.Account.ImageId         = "586a7d67cf43d7340cb54670";
                            var tradingDetails = new LetsTradingDetails {
                                Id = registerUser.Id, Credit = 100
                            };
                            DatabaseContext.RegisteredUsers.InsertOne(registerUser);
                            DatabaseContext.LetsTradingDetails.InsertOne(tradingDetails);

                            using (var mail = new MailMessage())
                            {
                                mail.To.Add(registerUser.Account.Email);
                                mail.Subject = "Welcome to Royal Holloway LETS";
                                mail.Body    = "<p>Hello " + registerUser.About.FirstName + ",</p><h3>Thanks for joining Royal Holloway LETS</h3><p>Please find your account details below</p><p>Title : <b>" + registerUser.About.Title + "</b></p><p>First Name : <b>" + registerUser.About.FirstName + "</b></p><p>Last Name : <b>" + registerUser.About.LastName + "</b></p><p>Gender : <b>" + registerUser.About.Gender + "</b></p><p>User Name : <b>" + registerUser.Account.UserName + "</b></p><p>Kind Regards,<br/>Royal Holloway LETS</p>";
                                SendEmail(mail);
                                TempData.Add("Registered", "You have successfully signed up for Royal Holloway LETS, We have also sent you can email with your account details for your future reference.");
                            }

                            return(RedirectToAction("Login"));
                        }
                        else
                        {
                            registerUser.Account.Password        = null;
                            registerUser.Account.ConfirmPassword = null;
                            ModelState.AddModelError("Account.Email", "Sorry, The following email already exists in our system.");
                            return(View(registerUser));
                        }
                    }
                    else
                    {
                        registerUser.Account.Password        = null;
                        registerUser.Account.ConfirmPassword = null;
                        ModelState.AddModelError("Account.UserName", "Sorry, This username is not available.");

                        if (userByEmail.Count > 0)
                        {
                            ModelState.AddModelError("Account.Email", "Sorry, The following email already exists in our system.");
                        }

                        return(View(registerUser));
                    }
                }
            }
            else
            {
                registerUser.Account.Password        = null;
                registerUser.Account.ConfirmPassword = null;
                ModelState.AddModelError("ReCaptcha", "Incorrect CAPTCHA entered.");
                return(View(registerUser));
            }
            return(View());
        }
示例#11
0
 public UserCtr()
 {
     userDB = new UserDB();
     passwordHashAndSalt = new PasswordHashAndSalt();
 }
示例#12
0
 public CompanyCtr()
 {
     companyDB           = new CompanyDB();
     passwordHashAndSalt = new PasswordHashAndSalt();
 }
        public IActionResult SaveTeacherRegistration(TeacherRegistrationVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View("TeacherRegistration", TeacherInput(model)));
            }

            var contact = new ContactInfo()
            {
                Email   = model.Email,
                Phone   = model.PhoneNummber,
                Address = model.Address
            };

            db.Contact.Add(contact);
            db.SaveChanges();

            var profileInfo = new ProfileInfo()
            {
                Username     = model.Username,
                PasswordSalt = PasswordHashAndSalt.GenerateSalt()
            };

            profileInfo.PasswordHash = PasswordHashAndSalt.GenerateHash(profileInfo.PasswordSalt, model.Password);

            db.ProfileInfo.Add(profileInfo);
            db.SaveChanges();

            var newTutor = new TutorRegistrationForm()
            {
                FName         = model.FName,
                LName         = model.LName,
                ProfileInfoId = profileInfo.Id,
                DateOfBirth   = model.DateOfBirth,
                CollageName   = model.CollageName,
                Price         = model.Price,
                TitleId       = model.TitleId,
                SubjectId     = model.SubjectId,
                ContactInfoId = contact.Id,
                CityId        = model.CityId,
                GenderId      = model.GenderId,
                IsRead        = false
            };



            if (model.ProfilePicture != null)
            {
                var fileExst     = Path.GetExtension(model.ProfilePicture.FileName);
                var newFileName  = Convert.ToString(Guid.NewGuid()) + fileExst;
                var fileName     = Path.Combine(hostingEnvironment.WebRootPath, "Profilepictures") + $@"\{newFileName}";
                var databaseName = "/Profilepictures/" + newFileName;
                model.ProfilePicture.CopyTo(new FileStream(fileName, FileMode.Create));
                newTutor.ProfilePicture = databaseName;
            }

            db.TutorRegistrationForm.Add(newTutor);
            db.SaveChanges();

            foreach (var item in model.typeOfStudents)
            {
                if (item.Checked)
                {
                    var PerferedType = new ListOfStudents()
                    {
                        TutorRegistrationFormId = newTutor.Id,
                        StudentTypeId           = item.StudentTypeId
                    };

                    db.ListOfStudents.Add(PerferedType);
                    db.SaveChanges();
                }
            }

            foreach (var item in model.Proof)
            {
                var fileExst    = Path.GetExtension(item.FileName);
                var newFileName = Convert.ToString(Guid.NewGuid()) + fileExst;
                var fileName    = Path.Combine(hostingEnvironment.WebRootPath, "ProofPictures") + $@"\{newFileName}";
                item.CopyTo(new FileStream(fileName, FileMode.Create));
                var databaseName = "/ProofPictures/" + newFileName;

                var proofPicture = new Proof()
                {
                    TutorRegistrationFormId = newTutor.Id,
                    PictureName             = databaseName
                };

                db.Proof.Add(proofPicture);
            }
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
 public UpdateDatabaseCtr()
 {
     passwordHashAndSalt            = new PasswordHashAndSalt();
     updateDB                       = new UpdateDatabase();
     sendTemperaryPasswordToCompany = new SendTemperaryPasswordToCompany();
 }