示例#1
0
        //默认登录界面
        public ActionResult Index(string appKey = "", string username = "")
        {
            TempData[AppInfo] = _appInfoService.Get(appKey);

            var viewModel = new PassportLoginRequest
            {
                AppKey = appKey,
                UserName = username,
            };

            return View(viewModel);
        }
示例#2
0
        public ActionResult Index(PassportLoginRequest model)
        {
            var result = Parse(model);

            if (result.Success)
            {
                var redirectUrl = string.Format("{0}?token={1}&sessionusername={2}", result.ReturnUrl, result.Token, model.UserName);

                //跳转默认回调页面
                return Redirect(redirectUrl);
            }

            return View(model);
        }
示例#3
0
        private LoginResult Parse(PassportLoginRequest model)
        {
            //过滤字段无效字符
            model.Trim();

            var result = new LoginResult();

            //获取应用信息
            var appInfo = _appInfoService.Get(model.AppKey);
            if (appInfo == null)
            {
                result.Success = false;
                result.ErrorMsg = "应用不存在";
            }
            TempData[AppInfo] = appInfo;

            //获取用户信息
            var userInfo = _appUserService.Get(model.UserName);
            if (userInfo == null)
            {
                result.Success = false;
                result.ErrorMsg = "用户不存在";
            }

            //if (userInfo.UserPwd != model.Password.ToMd5())
            //{
            //    //密码不正确
            //    return View(model);
            //}

            var currentSession = new UserAuthSession
            {
                UserName = model.UserName,
                Token = Guid.NewGuid().ToString().ToMd5(),
                InvalidTime = DateTime.Now.AddMinutes(10),
                AppKey = model.AppKey,
                CreateTime = DateTime.Now,
                IpAddress = Request.UserHostAddress
            };

            //创建Session
            new UserAuthSessionService().Create(currentSession);

            result.Success = true;
            result.ReturnUrl = appInfo.ReturnUrl;
            result.Token = currentSession.Token;
            return result;
        }
示例#4
0
 public string Check(PassportLoginRequest request)
 {
     return JsonConvert.SerializeObject(Parse(request));
 }
示例#5
0
        public ActionResult Index(PassportLoginRequest model)
        {
            //获取应用信息
            var appInfo = _appInfoService.Get(model.AppKey);
            if (appInfo == null)
            {
                //应用不存在
                return View(model);
            }

            TempData[AppInfo] = appInfo;

            if (ModelState.IsValid == false)
            {
                //实体验证失败
                return View(model);
            }

            //过滤字段无效字符
            model.Trim();

            //获取用户信息
            var userInfo = _appUserService.Get(model.UserName);
            if (userInfo == null)
            {
                //用户不存在
                return View(model);
            }

            if (userInfo.UserPwd != model.Password.ToMd5())
            {
                //密码不正确
                return View(model);
            }

            //获取当前未到期的Session
            var currentSession = _authSessionService.ExistsByValid(appInfo.AppKey, userInfo.UserName);
            if (currentSession == null)
            {
                //构建Session
                currentSession = new UserAuthSession
                {
                    AppKey = appInfo.AppKey,
                    CreateTime = DateTime.Now,
                    InvalidTime = DateTime.Now.AddYears(1),
                    IpAddress = Request.UserHostAddress,
                    SessionKey = Guid.NewGuid().ToString().ToMd5(),
                    UserName = userInfo.UserName
                };

                //创建Session
                _authSessionService.Create(currentSession);
            }
            else
            {
                //延长有效期,默认一年
                _authSessionService.ExtendValid(currentSession.SessionKey);
            }

            //记录用户授权日志
            _userAuthOperateService.Create(new UserAuthOperate
            {
                CreateTime = DateTime.Now,
                IpAddress = Request.UserHostAddress,
                Remark = string.Format("{0} 登录 {1} 授权成功", currentSession.UserName, appInfo.Title),
                SessionKey = currentSession.SessionKey
            });

            var redirectUrl = string.Format("{0}?SessionKey={1}&SessionUserName={2}",
                appInfo.ReturnUrl, 
                currentSession.SessionKey, 
                userInfo.UserName);

            //跳转默认回调页面
            return Redirect(redirectUrl);
        }