Esempio n. 1
0
        private void CheckPermission(PagedList <PostDto> posts)
        {
            var location = ClientIP.GetIPLocation() + "|" + Request.Headers[HeaderNames.UserAgent];

            posts.Data.RemoveAll(p => p.LimitMode switch
            {
                PostLimitMode.AllowRegion => !location.Contains(p.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot(),
                PostLimitMode.ForbidRegion => location.Contains(p.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot(),
                _ => false
            });
        public async Task <IActionResult> Redirect(int id)
        {
            var ad = await AdsService.GetByIdAsync(id) ?? throw new NotFoundException("推广链接不存在");

            if (!Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get <string>("ads" + id)))
            {
                HttpContext.Session.Set("ads" + id, id.ToString());
                ad.ClickRecords.Add(new AdvertisementClickRecord()
                {
                    IP       = ClientIP,
                    Location = ClientIP.GetIPLocation(),
                    Referer  = Request.Headers[HeaderNames.Referer].ToString(),
                    Time     = DateTime.Now
                });
                await AdsService.SaveChangesAsync();

                var start = DateTime.Today.AddMonths(-6);
                await ClickRecordService.GetQuery(a => a.Time < start).DeleteFromQueryAsync();
            }

            return(Redirect(ad.Url));
        }
        public async Task <ActionResult> RandomGo()
        {
            var ad = AdsService.GetByWeightedPrice((AdvertiseType) new Random().Next(1, 4), Request.Location());

            if (!Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get <string>("ads" + ad.Id)))
            {
                HttpContext.Session.Set("ads" + ad.Id, ad.Id.ToString());
                ad.ClickRecords.Add(new AdvertisementClickRecord()
                {
                    IP       = ClientIP,
                    Location = ClientIP.GetIPLocation(),
                    Referer  = Request.Headers[HeaderNames.Referer].ToString(),
                    Time     = DateTime.Now
                });
                await AdsService.SaveChangesAsync();

                var start = DateTime.Today.AddMonths(-1);
                await ClickRecordService.GetQuery(a => a.Time < start).DeleteFromQueryAsync();
            }

            return(Redirect(ad.Url));
        }
Esempio n. 4
0
        private void CheckPermission(Post post)
        {
            var location = ClientIP.GetIPLocation();

            switch (post.LimitMode)
            {
            case PostLimitMode.AllowRegion:
                if (!location.Contains(post.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot())
                {
                    throw new NotFoundException("文章未找到");
                }

                break;

            case PostLimitMode.ForbidRegion:
                if (location.Contains(post.Regions.Split(',', StringSplitOptions.RemoveEmptyEntries)) && !CurrentUser.IsAdmin && !VisitorTokenValid && !Request.IsRobot())
                {
                    throw new NotFoundException("文章未找到");
                }

                break;
            }
        }