Exemple #1
0
        public void AccountController_HttpPost_Login_Passing_Valid_Credentials_And_ReturnUrl_Redirects_To_Url()
        {
            var user              = FakeObjects.TestUserWithId();
            var returnUrl         = "stuff";
            var accountLoginInput = new AccountLoginInput()
            {
                Email = FakeValues.Email, Password = FakeValues.Password, ReturnUrl = returnUrl
            };

            using (var session = _documentStore.OpenSession())
            {
                session.Store(user);
                session.SaveChanges();
            }

            _mockCommandProcessor.Setup(x => x.Process <UserUpdateLastLoginCommand>(It.IsAny <UserUpdateLastLoginCommand>()));

            var result = _controller.Login(accountLoginInput);

            Assert.IsInstanceOf <RedirectToRouteResult>(result);
            Assert.IsTrue(((RedirectToRouteResult)result).RouteValues.ContainsKey("action"));
            Assert.AreEqual("loggingin", ((RedirectToRouteResult)result).RouteValues["action"].ToString());
            Assert.IsTrue(((RedirectToRouteResult)result).RouteValues.ContainsKey("returnUrl"));
            Assert.AreEqual(returnUrl, ((RedirectToRouteResult)result).RouteValues["returnUrl"].ToString());
        }
Exemple #2
0
        public void AccountController_HttpPost_Login_Passing_Valid_Credentials_Signs_User_In_And_Updates_Last_Logged_In_And_Redirects_To_Logging_In_Action()
        {
            var user = FakeObjects.TestUserWithId();

            var accountLoginInput = new AccountLoginInput()
            {
                Email = FakeValues.Email, Password = FakeValues.Password
            };

            using (var session = _documentStore.OpenSession())
            {
                session.Store(user);
                session.SaveChanges();
            }

            _mockCommandProcessor.Setup(x => x.Process <UserUpdateLastLoginCommand>(It.IsAny <UserUpdateLastLoginCommand>()));

            var result = _controller.Login(accountLoginInput);

            _mockUserContext.Verify(x => x.SignUserIn(It.IsAny <string>(), It.IsAny <bool>()), Times.Once());
            _mockCommandProcessor.Verify(x => x.Process <UserUpdateLastLoginCommand>(It.IsAny <UserUpdateLastLoginCommand>()), Times.Once());

            Assert.IsInstanceOf <RedirectToRouteResult>(result);
            Assert.IsTrue(((RedirectToRouteResult)result).RouteValues.ContainsKey("action"));
            Assert.AreEqual("loggingin", ((RedirectToRouteResult)result).RouteValues["action"].ToString());
        }
Exemple #3
0
        public void AccountController_HttpPost_Login_Passing_Invalid_Credentials_Loads_LoginViewModel()
        {
            var accountLogin      = new { Email = string.Empty };
            var accountLoginInput = new AccountLoginInput()
            {
                Email = FakeValues.Email, Password = FakeValues.Password
            };

            _controller.Login(accountLoginInput);
        }
Exemple #4
0
        public Account Login(AccountLoginInput input)
        {
            Assert.IfNullOrWhiteSpaceThrow(input.Username, "用户名不能为空");
            Assert.IfNullOrWhiteSpaceThrow(input.Password, "密码不能为空");

            var account = _accountRepository.Table.FirstOrDefault(x => x.Username == input.Username);

            Assert.IfNullThrow(account, "用户不存在");

            Assert.IfTrueThrow(input.Password.ToMD5() != account.Password, "密码错误");

            return(account);
        }
        public ActionResult Login(AccountLoginInput accountLoginInput)
        {
            User    user      = null;
            dynamic viewModel = new ExpandoObject();

            if (ModelState.IsValid)
            {
                if (AreCredentialsValid(accountLoginInput.Email, accountLoginInput.Password, out user))
                {
                    _messageBus.Send(
                        new UserUpdateLastLoginCommand()
                    {
                        Email = accountLoginInput.Email
                    });

                    _userContext.SignUserIn(user.Id, user.Email, accountLoginInput.RememberMe);

                    if (Request.IsAjaxRequest())
                    {
                        viewModel.User = _userViewFactory.Make(user, user);

                        return(RestfulResult(
                                   viewModel,
                                   "account",
                                   "login"));
                    }
                    else
                    {
                        return(RedirectToAction("loggingin", new { returnUrl = accountLoginInput.ReturnUrl }));
                    }
                }
                else
                {
                    Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden;

                    ModelState.AddModelError("CredentialsInvalid", I18n.CredentialsInvalid);
                }
            }
            else
            {
                Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
            }

            viewModel.AccountLogin = accountLoginInput;

            return(RestfulResult(
                       viewModel,
                       "account",
                       "login"));
        }
Exemple #6
0
        public void AccountController_HttpPost_Login_Passing_Invalid_Credentials_Returns_Login_View()
        {
            var accountLogin      = new { Email = string.Empty };
            var accountLoginInput = new AccountLoginInput()
            {
                Email = FakeValues.Email, Password = FakeValues.Password
            };

            _controller.Login(accountLoginInput);

            var viewModel = _controller.ViewData.Model;

            Assert.IsInstanceOf <object>(viewModel);
        }
Exemple #7
0
        public ActionResult <UserTokenOutput> Login(AccountLoginInput input)
        {
            UserTokenOutput output = null;

            try
            {
                var user = _accountService.Login(input);

                output = _authorizationManager.UserToken(user.Username);
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }

            return(output);
        }
Exemple #8
0
        public async Task <ActionRes> LoginAsync([FromBody] AccountLoginInput input)
        {
            if (input.UserName != _configuration["Account"] || input.Password != _configuration["AccountPwd"])
            {
                throw new UserFriendlyException("登录失败,用户账号或密码错误!");
            }

            long   userId = 1;
            string token  = "";

            token = GetToken(userId, input.UserName);

            var data = new
            {
                status           = "ok",
                type             = input.Type,
                currentAuthority = "administrator",
                token,
                UserId   = userId,
                NickName = input.UserName
            };

            return(ActionRes.Success(data));
        }