Example #1
0
        public void Create(SSO_UserAuthSessions model, View_Sys_UserInfo UserInfo)
        {
            //添加Session
            UserAuthSessionsBLL.Add(model);

            //设置缓存
            CacheContext.Set(model.SessionKey, new SessionCacheItem
            {
                AppKey         = model.AppKey,
                InvalidTime    = model.InvalidTime,
                UserName       = model.UserName,
                UserID         = UserInfo.UserInfoID,
                LoginUserName  = UserInfo.UserInfo_LoginUserName,
                DepartmentName = UserInfo.Department_Name,
                DepartmentCode = UserInfo.UserInfo_DepCode,
                PostID         = UserInfo.UserInfo_Post,
                PostName       = UserInfo.UserInfo_PostName,
                RoleID         = UserInfo.UserInfo_RoleID,
                RoleName       = UserInfo.UserInfo_RoleName,
                UserType       = UserInfo.UserInfo_Type,
                DepType        = UserInfo.Department_Type
            });
        }
Example #2
0
        public ActionResult Index(PassportLoginRequest model)
        {
            //获取应用信息
            var appInfo = _appInfoService.Get(p => p.Id == model.AppKey);

            if (appInfo == null)
            {
                //应用不存在
                return(Content(new Shu.Comm.JsonResult().SetError(true).SetMsg("应用不存在。").ToJson()));
                //return View(model);
            }
            if (Session["shu_session_verifycode"].IsNullOrEmpty() || model.Code.ToLower().MD5Encrypt() != Session["shu_session_verifycode"].ToString())
            {
                return(Content(new Shu.Comm.JsonResult().SetError(true).SetMsg("验证码错误,请重新输入").ToJson()));
            }

            TempData[AppInfo] = appInfo;

            if (ModelState.IsValid == false)
            {
                //实体验证失败
                return(Content(new Shu.Comm.JsonResult().SetError(true).SetMsg("实体验证失败").ToJson()));
            }

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

            //获取用户信息
            var userInfo = _appUserService.Get(p => p.UserInfo_LoginUserName == model.UserName);

            if (userInfo == null)
            {
                //用户不存在
                return(Content(new Shu.Comm.JsonResult().SetError(true).SetMsg("用户不存在").ToJson()));
            }

            if (userInfo.UserInfo_LoginUserPwd != HttpUtility.UrlDecode(DESEncrypt.Encrypt(model.Password)))
            {
                //密码不正确
                return(Content(new Shu.Comm.JsonResult().SetError(true).SetMsg("密码不正确").ToJson()));
            }

            //获取当前未到期的Session
            var currentSession = _authSessionService.ExistsByValid(appInfo.Id, userInfo.UserInfo_LoginUserName);

            if (currentSession == null)
            {
                //构建Session
                currentSession = new SSO_UserAuthSessions
                {
                    AppKey      = appInfo.Id,
                    CreateTime  = DateTime.Now,
                    InvalidTime = DateTime.Now.AddYears(1),
                    IpAddress   = Request.UserHostAddress,
                    SessionKey  = Guid.NewGuid().ToString().ToMd5(),
                    UserName    = userInfo.UserInfo_LoginUserName
                };

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

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

            string DomainUrl = string.Empty;

#if DEBUG
            DomainUrl = ConfigurationManager.AppSettings["DomainUrl"];
#else
            DomainUrl = appInfo.DomainUrl;
#endif
            var redirectUrl = string.Format("{0}?SessionKey={1}&SessionUserName={2}",
                                            DomainUrl,
                                            currentSession.SessionKey,
                                            userInfo.UserInfo_LoginUserName);

            return(Content(new Shu.Comm.JsonResult().SetError(false).SetMsg("登录成功。").SetData(redirectUrl).ToJson()));
            //跳转默认回调页面
            //return Redirect(redirectUrl);
        }