Esempio n. 1
0
        public void CreateUserTest()
        {
            try
            {
                var userRepository = _serviceProvider.GetRequiredService <IUserRepository>();

                var userName = "******";
                var password = "******";

                var user = new User()
                {
                    Id           = Guid.NewGuid().ToString(),
                    UserName     = userName,
                    PasswordHash = ApplicationPasswordHasher.CreateHashPassword(password)
                };

                var success = userRepository.CreateUser(user);

                Assert.AreEqual(true, success);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public async Task <ActionResult> Add(Admin admin)
        {
            if (ModelState.IsValid)
            {
                var userRepository = new ApplicationUserRepository <Admin>();
                var roleRepository = new ApplicationRoleRepository();
                var role           = roleRepository.GetByNameAsync("admin");
                var isUserExist    = userRepository.IsUserExistAsync(admin.UserName);

                if (await isUserExist)
                {
                    TempData["error"] = "Account with this name already exist!";
                    return(View("Create", admin));
                }

                Admin _admin = new Admin()
                {
                    UserName     = admin.UserName,
                    Email        = admin.Email,
                    Role         = await role,
                    PasswordHash = ApplicationPasswordHasher.HashPasword(admin.PasswordHash),
                    FirstName    = admin.FirstName,
                    LastName     = admin.LastName
                };

                new ApplicationUserRepository <Admin>().Create(_admin);
                TempData["success"] = "Admin account was added";
            }
            else
            {
                return(View("Create", admin));
            }

            return(RedirectToAction("Admins"));
        }
Esempio n. 3
0
        public async Task <ActionResult> RegisterTester(Tester tester)
        {
            ModelState.Remove(nameof(tester.FirstName));
            ModelState.Remove(nameof(tester.LastName));

            if (ModelState.IsValid)
            {
                var userRepository = new ApplicationUserRepository <ApplicationUser>();
                var roleRepository = new ApplicationRoleRepository();
                var role           = roleRepository.GetByNameAsync("tester");
                var isUserExist    = userRepository.IsUserExistAsync(tester.UserName);

                if (await isUserExist)
                {
                    TempData["error"] = "Account with this name already exist!";
                    return(View("Tester"));
                }

                Tester _tester = new Tester()
                {
                    UserName     = tester.UserName,
                    Email        = tester.Email,
                    Role         = await role,
                    PasswordHash = ApplicationPasswordHasher.HashPasword(tester.PasswordHash)
                };

                _tester.Address = new Address()
                {
                    User = _tester
                };
                new ApplicationUserRepository <Tester>().Create(_tester);

                ClaimsIdentity identity = await AppUserManager.CreateIdentityAsync(_tester, DefaultAuthenticationTypes.ApplicationCookie);

                identity.AddClaim(new Claim(ClaimTypes.Role, _tester.Role.Name));

                AuthManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                AuthManager.SignIn(new AuthenticationProperties()
                {
                    AllowRefresh = true,
                    IsPersistent = false,
                    ExpiresUtc   = DateTime.UtcNow.AddMinutes(10)
                }, identity);

                return(RedirectToAction("Index", "Home", new { area = "Testing" }));
            }

            return(View("Tester"));
        }
        private static void TestPasswordHashing()
        {
            var password = "******";
            var items = new Dictionary<int, KeyValuePair<string, string>>();
            var hasher = new ApplicationPasswordHasher();

            for (var i = 0; i < 10; i++)
            {
                items.Add(i, new KeyValuePair<string, string>(password, hasher.HashPassword(password)));
            }

            foreach (var item in items)
            {
                System.Console.WriteLine(hasher.VerifyHashedPassword(item.Value.Value, password));
            }
        }
Esempio n. 5
0
        public async Task <ActionResult> RegisterCompany(Company company)
        {
            ModelState.Remove(nameof(company.Rating));
            ModelState.Remove(nameof(company.Reviews));
            ModelState.Remove(nameof(company.CompanyName));

            if (ModelState.IsValid)
            {
                var userRepository = new ApplicationUserRepository <ApplicationUser>();
                var roleRepository = new ApplicationRoleRepository();
                var role           = roleRepository.GetByNameAsync("company");
                var isUserExist    = userRepository.IsUserExistAsync(company.UserName);

                if (await isUserExist)
                {
                    TempData["error"] = "Account with this name already exist!";
                    return(View("Company"));
                }

                Company _company = new Company()
                {
                    UserName     = company.UserName,
                    Email        = company.Email,
                    Role         = await role,
                    PasswordHash = ApplicationPasswordHasher.HashPasword(company.PasswordHash),
                    Address      = new Address()
                };

                new ApplicationUserRepository <Company>().Create(_company);

                ClaimsIdentity identity = await AppUserManager.CreateIdentityAsync(_company, DefaultAuthenticationTypes.ApplicationCookie);

                AuthManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                AuthManager.SignIn(new AuthenticationProperties()
                {
                    AllowRefresh = true,
                    IsPersistent = false,
                    ExpiresUtc   = DateTime.UtcNow.AddMinutes(10)
                }, identity);

                return(RedirectToAction("Index", "Home", new { area = "Testing" }));
            }

            return(View("Company"));
        }
 public AccountController(IUserRepository userRepository)
 {
     _applicationUserManager    = new ApplicationUserManager(userRepository);
     _applicationPasswordHasher = new ApplicationPasswordHasher();
 }