示例#1
0
        public void OnPageHandlerExecuting(PageHandlerExecutingContext context)
        {
            if (!context.HttpContext.User.Identity.IsAuthenticated)
            {
                return;
            }
            MethodInfo method = context.GetHandlerMethod();

            if (method == null || method.CustomAttributes.Any(x => x.AttributeType == typeof(IgnorePageLockAttribute)))
            {
                return;
            }

            var tokenId = context.HttpContext.User.GetTokenIdOrNull();
            var isLock  = _cacheService.Get <bool>(string.Format(CacheKeyDefinition.PageLock, tokenId));

            if (isLock)
            {
                if (context.HttpContext.Request.IsJsonRequest())
                {
                    context.Result = new JsonResult(new
                    {
                        code    = 905,
                        message = "页面已锁定"
                    });
                }
                else
                {
                    var url = context.HttpContext.Request.Path.Value;
                    context.Result = new RedirectResult("/Account/Lock?ReturnUrl=" + WebUtility.UrlEncode(url));
                }
            }
        }
        public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
        {
            await next();

            await Task.Factory.StartNew(async() => {
                var method = context.GetHandlerMethod();
                var attr   = method.GetCustomAttribute <PageViewRecordAttribute>(true);
                if (attr != null)
                {
                    await _service.SaveAsync(new PageViewRecordDto {
                        SessionId = context.HttpContext.Session.Id,
                        PageName  = attr.PageName,
                        Url       = context.HttpContext.RequestPath(),
                        Referer   = context.HttpContext.UrlReferrer(),
                        UserAgent = context.HttpContext.UserAgent().SubstringByByte(128),
                        ClientIp  = context.HttpContext.IpAddress(),
                    });
                }
            });
        }