public void Create(UserAuthSession model)
        {
            //添加Session
            DbContext.UserAuthSession.Add(model);
            DbContext.SaveChanges();

            //设置缓存
            CacheContext.Set(model.SessionKey, new SessionCacheItem
            {
                AppKey = model.AppKey,
                InvalidTime = model.InvalidTime,
                UserName = model.UserName
            });
        }
Exemple #2
0
        public void TestMethod1()
        {
            var currentSession = new UserAuthSession
            {
                UserName = "******",
                Token = Guid.NewGuid().ToString().ToMd5(),
                InvalidTime = DateTime.Now.AddMinutes(10),
                AppKey = "670b14728ad9902aecba32e22fa4f6bd",
                CreateTime = DateTime.Now,
                IpAddress = "192.168.2.121"
            };
            _authSessionService.Create(currentSession );

            var obj = _authSessionService.Get(currentSession.Token);

            Assert.IsTrue(obj != null);

            Console.Write(obj.UserName +"  " + obj.Token);
        }
 public bool Create(UserAuthSession model)
 {
     //设置缓存
     return CacheContext.Set(model.Token, model);
 }
Exemple #4
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;
        }
        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);
        }