예제 #1
0
        public void InsertUserTest()
        {
            User user = new User
            {
                FirstName        = "TestUser",
                LastName         = "User",
                Email            = "*****@*****.**",
                UserName         = "******",
                LastLoginTime    = DateTime.UtcNow,
                RegistrationDate = DateTime.UtcNow,
                EmailConfirmed   = true,
                PasswordHash     = (new PasswordHasher()).HashPassword("test")
            };


            var res = _userManager.CreateAsync(user).Result;

            if (res == IdentityResult.Success)
            {
                var roleres = _userManager.AddToRoleAsync(user.Id, "Employee").Result;
            }
            _unitOfWork.SaveChanges();
            user = _userManager.FindByNameAsync("testuser").Result;
            Assert.IsNotNull(user);
            //var paiidParameter = new SqlParameter("@Paid", userweekpaiDto.Id);
            //var paivalueParameter = new SqlParameter("@PaiValue", userweekpaiDto.Paiment);

            //context.Database.ExecuteSqlCommand("exec UpdateWeekPaiment @Paid,PaiValue",
            //    paiidParameter, paivalueParameter);
        }
예제 #2
0
 public static AccountDto MapDto(User user)
 {
     return new AccountDto
     {
         UserId = user.Id,
         Email = user.Email,
         FullName = user.LastName + " " + user.FirstName,
         LastLoginTime = user.LastLoginTime.ToShortDateString(),
         RegistrationDate = user.RegistrationDate.ToShortDateString(),
         CheckDebt = user.CheckDebt,
         IsExisting = user.IsExisting,
         Balance = user.Balance
     };
 }
예제 #3
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // Сбои при входе не приводят к блокированию учетной записи
            // Чтобы ошибки при вводе пароля инициировали блокирование учетной записи, замените на shouldLockout: true
            //var result = await SignInManager.ValidateUserFromAd(model.LogIn, model.Password);
            var result = await _signInManager.PasswordSignInAsync(model.LogIn, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    var user = UserManager.FindByName(model.LogIn);
                    if (user != null)
                    {
                        var passres =
                            await
                                _signInManager.PasswordSignInAsync(model.LogIn, model.Password, model.RememberMe,
                                    shouldLockout: false);
                        if (passres == SignInStatus.Success)
                        {
                            if (await UserManager.IsInRoleAsync(user.Id, "Employee"))
                            {
                                user.LastLoginTime = DateTime.UtcNow;
                                Session["EmployeeFullname"] = user.LastName + " " + user.FirstName;
                                user.LastLoginTime = DateTime.Now;
                                await
                                    _signInManager.PasswordSignInAsync(model.LogIn, model.Password, model.RememberMe,
                                        shouldLockout: false);

                                return RedirectToAction("Index", "Employee", new {Area = "EmployeeArea"});
                            }
                            if (await UserManager.IsInRoleAsync(user.Id, "SuperUser"))
                            {
                                user.LastLoginTime = DateTime.UtcNow;
                                Session["FullName"] = user.LastName + " " + user.FirstName;
                                user.LastLoginTime = DateTime.Now;
                                await
                                    _signInManager.PasswordSignInAsync(model.LogIn, model.Password, model.RememberMe,
                                        shouldLockout: false);

                                return RedirectToAction("WeekMenu", "SU_", new {Area = "SU_Area"});
                            }

                        }
                        else
                        {
                            User userchangePass = UserManager.FindByName(model.LogIn);
                            if (userchangePass != null)
                            {
                                userchangePass.PasswordHash = _userManager.PasswordHasher.HashPassword(model.Password);
                                var updateres = _userManager.Update(userchangePass);
                                if (updateres == IdentityResult.Success)
                                {
                                    await Login(model, returnUrl);

                                }
                            }
                        }
                    }
                    else
                    {
                        UserPrincipal u = new UserPrincipal(_ad) {SamAccountName = model.LogIn};
                        PrincipalSearcher search = new PrincipalSearcher(u);
                        UserPrincipal usprincrezult = (UserPrincipal) search.FindOne();
                        search.Dispose();
                        if (usprincrezult != null)
                        {
                            user = new User
                            {
                                FirstName = usprincrezult.GivenName,
                                LastName = usprincrezult.Surname,
                                Email = usprincrezult.EmailAddress,
                                UserName = usprincrezult.SamAccountName,
                                LastLoginTime = DateTime.UtcNow,
                                RegistrationDate = DateTime.UtcNow,
                                EmailConfirmed = true,
                                PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password),
                                CheckDebt = true,
                                IsExisting = true
                            };
                        }

                        var res = UserManager.CreateAsync(user, model.Password).Result;
                        if (res == IdentityResult.Success)
                        {
                            if (user != null) await UserManager.AddToRoleAsync(user.Id, "Employee");


                            var signres = await
                                _signInManager.PasswordSignInAsync(model.LogIn, model.Password, model.RememberMe,
                                    shouldLockout: false);

                            user = await UserManager.FindByNameAsync(model.LogIn);

                            if (user != null && signres == SignInStatus.Success)
                            {
                                user.LastLoginTime = DateTime.UtcNow;
                                Session["EmployeeFullname"] = user.LastName + " " + user.FirstName;
                                if (!user.Email.IsNullOrWhiteSpace())
                                {
                                    MessageService.SendEmailAsync(new List<User>(new[] {user}),
                                        MessageTopic.Registration);
                                }
                            }
                            return RedirectToAction("Index", "Employee", new {Area = "EmployeeArea"});
                        }
                    }
                    return RedirectToLocal(returnUrl);

                case SignInStatus.LockedOut:
                    ModelState.AddModelError("", "Ваша учётная запись заблокирована.");
                    return View(model);
                    
                case SignInStatus.Failure:
                    var specuser = UserManager.FindByName(model.LogIn);
                    if (specuser != null)
                    {

                        if (await UserManager.IsInRoleAsync(specuser.Id, "SuperUser"))
                        {
                            specuser.LastLoginTime = DateTime.UtcNow;
                            Session["FullName"] = specuser.LastName + " " + specuser.FirstName;
                            specuser.LastLoginTime = DateTime.Now;
                            await
                                _signInManager.PasswordSignInAsync(model.LogIn, model.Password, model.RememberMe,
                                    shouldLockout: false);

                            return RedirectToAction("WeekMenu", "SU_", new {Area = "SU_Area"});
                        }
                        if (await UserManager.IsInRoleAsync(specuser.Id, "Employee"))
                        {
                            specuser.LastLoginTime = DateTime.UtcNow;
                            Session["EmployeeFullname"] = specuser.LastName + " " + specuser.FirstName;
                            specuser.LastLoginTime = DateTime.Now;
                            await
                                _signInManager.PasswordSignInAsync(model.LogIn, model.Password, model.RememberMe,
                                    shouldLockout: false);

                            return RedirectToAction("Index", "Employee", new {Area = "EmployeeArea"});
                        }
                    }

                    ModelState.AddModelError("", "Неудачная попытка входа.");
                    return View(model);

                default:
                    ModelState.AddModelError("", "Неудачная попытка входа.");
                    return View(model);
            }
        }
예제 #4
0
        public void InsertUserTest()
        {
            User  user = new User
                            {
                                FirstName = "TestUser",
                                LastName = "User",
                                Email = "*****@*****.**",
                                UserName = "******",
                                LastLoginTime = DateTime.UtcNow,
                                RegistrationDate = DateTime.UtcNow,
                                EmailConfirmed = true,
                                PasswordHash = (new PasswordHasher()).HashPassword("test")
                            };

            var res = _userManager.CreateAsync(user).Result;
            if (res == IdentityResult.Success)
            {
                var roleres= _userManager.AddToRoleAsync(user.Id, "Employee").Result;
            }
            _unitOfWork.SaveChanges();
            user = _userManager.FindByNameAsync("testuser").Result;
            Assert.IsNotNull(user);
            //var paiidParameter = new SqlParameter("@Paid", userweekpaiDto.Id);
            //var paivalueParameter = new SqlParameter("@PaiValue", userweekpaiDto.Paiment);

            //context.Database.ExecuteSqlCommand("exec UpdateWeekPaiment @Paid,PaiValue",
            //    paiidParameter, paivalueParameter);
        }