예제 #1
0
        public void AddLogin(SaveLoginAct request, string editor, Guid merchantId)
        {
            if (string.IsNullOrEmpty(request.Login))
            {
                throw new BaseException("账号不能为空!");
            }

            if (string.IsNullOrEmpty(request.Password))
            {
                throw new BaseException("密码不能为空!");
            }

            var count = _sysLoginRepository.Count(e => e.Login == request.Login && e.MerchantID == merchantId);

            if (count > 0)
            {
                throw new BaseException("账号已存在!");
            }

            SysLogin login = new SysLogin()
            {
                LoginID    = Guid.NewGuid(),
                MerchantID = merchantId,
                Login      = request.Login,
                Name       = request.Name,
                Status     = (int)Status.Enable,
                Password   = EncryptHelper.SHA1Hash(request.Password),
                Creater    = editor,
                Editor     = editor,
                CreateTime = DateTime.Now,
                EditTime   = DateTime.Now,
                Tele       = request.Tele
            };

            _sysLoginRepository.Add(login);

            if (request.RoleId.SafeValue() != Guid.Empty)
            {
                SysLoginRole loginRole = new SysLoginRole()
                {
                    LoginID     = login.LoginID,
                    LoginRoleID = Guid.NewGuid(),
                    MerchantID  = merchantId,
                    RoleID      = request.RoleId.SafeValue()
                };
                _sysLoginRoleRepository.Add(loginRole);
            }
        }
예제 #2
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            // Log the login
            SysLogin login = new SysLogin();

            login.Success   = false;
            login.Timestamp = DateTime.Now;
            login.User      = db.Users.SingleOrDefault(x => x.UserName == model.Username);
            login.Username  = model.Username;


            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout : false);

            if (result == SignInStatus.Success)
            {
                login.Success = true;
            }


            db.Logins.Add(login);
            db.SaveChanges();

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }