Ejemplo n.º 1
0
        public LoginResponse Login([FromBody]LoginRequest user)
        {
            if (user == null)
            {
                throw new BadRequestException();
            }

            var response = new LoginResponse();

            UserBusiness ub = new UserBusiness();
            UserModel userModel = null;
            bool isLogin = ub.Login(user.UserName, user.Password, out userModel, false, Context.ClientIP);

            if (!isLogin)
            {
                throw new UnauthorizedException("非法的用户名或密码!");
            }
            else
            {
                response.Token = userModel.Token;
                response.IsAdministrator = userModel.UserType == 0;
                // add login record
                //LoginRecordRepository recordRep = new LoginRecordRepository();
                //LoginRecord loginRecord = new LoginRecord
                //{
                //    IsLoginFromClient = 1,
                //    LoginIP = Context.ClientIP,
                //    UserName = user.UserName
                //};
                //recordRep.Add(loginRecord);
            }

            return response;
        }
Ejemplo n.º 2
0
        public ActionResult Index(LoginRequest loginModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if(String.IsNullOrEmpty(loginModel.UserName) || String.IsNullOrEmpty(loginModel.Password))
                    {
                        ModelState.AddModelError("", "请输入用户名和密码!");
                        return View(loginModel);
                    }

                    UserBusiness userBiz = new UserBusiness();
                    UserModel userModel = null;
                    bool isLoginSuccess = userBiz.Login(loginModel.UserName, loginModel.Password, out userModel,true, Context.ClientIP);

                    if (isLoginSuccess)
                    {
                        SessionManager.IsLogin = true;
                        SessionManager.User = userModel;
                        string redirectUrl = Request.QueryString["returnUrl"];
                        if (!String.IsNullOrWhiteSpace(redirectUrl))
                        {
                            return Redirect(redirectUrl);
                        }
                        else
                        {
                            return RedirectToAction("MyInfo", "User");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "用户名或密码不正确!");
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(loginModel);
        }