コード例 #1
0
ファイル: BaseShared.cs プロジェクト: zhangzhg0508/Cnblogs
        public void Set(string key, string value)
        {
            ISharedPreferencesEditor editor = sp.Edit();

            try
            {
                string spValue = DESUtils.Encrypt(value.ToString(), GetSecretKey());
                editor.PutString(GetDigestKey(key), spValue);
            }
            catch (Exception e)
            {
                editor.PutString(GetDigestKey(key), "");
            }
            editor.Apply();
        }
コード例 #2
0
        public ActionResult ModifyUserPwd(ChangePasswordViewModel viewModel)
        {
            int userId = UserManager.GetCurrentUserInfo().Id;
            var result = WriteError("出现异常,密码修改失败");

            if (!SysUserService.CheckLogin(viewModel.UserName, viewModel.OldPassword))
            {
                return(WriteError("旧密码不正常"));
            }
            else
            {
                if (SysUserService.ModifyUserPwd(userId, viewModel))
                {
                    result = WriteSuccess("密码修改成功");
                    List <string> list = DESUtils.Decrypt(CookieHelper.Get(KeyManager.IsMember)).ToList <string>();
                    if (list == null || list.Count() != 2)
                    {
                        //获取缓存的用户信息
                        SysUser userinfo = CacheManager.Get <SysUser>(list[0]);
                        //删除缓存的用户信息
                        CacheManager.Remove(list[0]);
                        //更新缓存用户信息的KEY
                        list[0] = Guid.NewGuid().ToString();
                        if (list[1] == "0")
                        {
                            CacheManager.Set(list[0], userinfo, new TimeSpan(10, 0, 0, 0));
                            CookieHelper.Set(KeyManager.IsMember, DESUtils.Encrypt(list.ToJson()));
                        }
                        else if (list[1] == "1")
                        {
                            CacheManager.Set(list[0], userinfo, new TimeSpan(0, 30, 0));
                            CookieHelper.Set(KeyManager.IsMember, DESUtils.Encrypt(list.ToJson()), 30);
                        }
                    }
                }
                else
                {
                    result = WriteError("密码修改失败");
                }
            }
            return(result);
        }
コード例 #3
0
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            //判断是否有贴跳过登录检查的特性标签(控制器)
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }

            //方法
            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }

            if (!string.IsNullOrEmpty(CookieHelper.Get(KeyManager.IsMember)))
            {
                List <string> list = DESUtils.Decrypt(CookieHelper.Get(KeyManager.IsMember)).ToList <string>();
                if (list == null || list.Count() != 2)
                {
                    ToLogin(filterContext);
                    return;
                }
                SysUser userinfo = CacheManager.Get <SysUser>(list[0]);
                if (userinfo != null)
                {
                    // 0为永久key
                    if (list[1] == "0")
                    {
                        CacheManager.Set(list[0], userinfo, new TimeSpan(10, 0, 0, 0));
                    }
                    // 1为滑动key
                    else if (list[1] == "1")
                    {
                        CacheManager.Set(list[0], userinfo, new TimeSpan(0, 30, 0));
                        CookieHelper.Set(KeyManager.IsMember, DESUtils.Encrypt(list.ToJson()), 30);
                    }
                    else
                    {
                        ToLogin(filterContext);
                        return;
                    }
                }
                else
                {
                    ToLogin(filterContext);
                    return;
                }
            }
            else
            {
                ToLogin(filterContext);
                return;
            }

            //获得当前要执行的Action上标注的CheckPermissionAttribute实例对象
            CheckPermission[] permAtts = (CheckPermission[])filterContext.ActionDescriptor
                                         .GetCustomAttributes(typeof(CheckPermission), false);
            if (permAtts.Length <= 0)
            {
                return;
            }

            var container = CacheManager.Get <IContainer>(KeyManager.AutofacContainer);
            ISysActionService sysActionService = container.Resolve <ISysActionService>();

            //检查是否有权限
            foreach (var permAtt in permAtts)
            {
                //判断当前登录用户是否具有permAtt.Permission权限
                if (!sysActionService.HasPermission(UserManager.GetCurrentUserInfo().Id, permAtt.Permission))
                {
                    NoPermission(filterContext);
                    return;
                }
            }
        }
コード例 #4
0
 public ActionResult Login(LoginViewModel loginViewModel)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(WriteError("实体验证失败"));
         }
         if (loginViewModel.VerifyCode.ToLower() != (string)TempData["verifyCode"])
         {
             return(WriteError("验证码失败"));
         }
         bool result = SysUserService.CheckLogin(loginViewModel.LoginName, loginViewModel.LoginPassword);
         if (result)
         {
             var    user        = SysUserService.GetList(x => x.UserName == loginViewModel.LoginName).FirstOrDefault();
             string userLoginId = Guid.NewGuid().ToString();
             if (loginViewModel.IsMember)
             {
                 List <string> list = new List <string>
                 {
                     userLoginId,
                     "0"
                 };
                 CookieHelper.Set(KeyManager.IsMember, DESUtils.Encrypt(list.ToJson()));
                 CacheManager.Set(userLoginId, user, new TimeSpan(10, 0, 0, 0));
             }
             else
             {
                 CookieHelper.Remove(KeyManager.IsMember);
                 List <string> list = new List <string>
                 {
                     userLoginId,
                     "1"
                 };
                 CookieHelper.Set(KeyManager.IsMember, DESUtils.Encrypt(list.ToJson()), 30);
                 CacheManager.Set(userLoginId, user, new TimeSpan(0, 30, 0));
             }
             SysLogLogonService.WriteDbLog(new SysLogLogon
             {
                 LogType     = DbLogType.Login.ToString(),
                 Account     = user.UserName,
                 RealName    = user.NickName,
                 Description = "登陆成功",
             });
             return(WriteSuccess("登录成功"));
         }
         else
         {
             return(WriteError("用户名或者密码错误"));
         }
     }
     catch (Exception ex)
     {
         SysLogLogonService.WriteDbLog(new SysLogLogon
         {
             LogType     = DbLogType.Exception.ToString(),
             Account     = loginViewModel.LoginName,
             RealName    = loginViewModel.LoginName,
             Description = "登录失败," + ex.Message
         });
         return(WriteError(ex));
     }
 }