public IActionResult PictureUpload(PictureDto body) { using EFCoreContextWrite context = new EFCore.EFCoreContextWrite(); if (!string.IsNullOrEmpty(body.PictureTitle) && !string.IsNullOrEmpty(body.PictureExplain)) { string token = _httpContext.HttpContext.Request.Headers["Authorization"]; AuthRedis.GetUserByToken(token, out UserInfo userInfo); PictureInfo PictureInfos = new PictureInfo() { Id = SequenceID.GetSequenceID(), CreateTime = DateTime.Now, Disable = false, PictureContent = body.url, UserID = userInfo.id, LastModifiedTime = DateTime.Now, PictureExplain = body.PictureExplain, PictureTitle = body.PictureTitle, RecommendIndex = body.Index, PictureType = body.PictureType, PhotoType = body.PhotoType }; context.Add(PictureInfos); context.SaveChanges(); PictureRedis.Del(); } return(Ok(new ApiResponse())); }
public override void OnActionExecuted(ActionExecutedContext context) { RequestLogs logs = new RequestLogs() { Id = SequenceID.GetSequenceID(), ApiName = context.HttpContext.Request.GetEncodedUrl(), IP = context.HttpContext.Connection.RemoteIpAddress.ToString(), Headers = JsonConvert.SerializeObject(context.HttpContext.Request.Headers), QueryString = context.HttpContext.Request.QueryString.Value, StatusCode = context.HttpContext.Response.StatusCode, Result = JsonConvert.SerializeObject(context.Result) }; string token = context.HttpContext.Request.Headers["Authorization"]; if (!string.IsNullOrEmpty(token)) { if (AuthRedis.GetUserByToken(token, out UserInfo userInfo)) { logs.UID = userInfo.id; } } DBRequestLogs _logs = new DBRequestLogs(); _logs.Create(logs); }
[AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口 public IActionResult TestAuth() { string token = _httpContext.HttpContext.Request.Headers["Authorization"]; if (AuthRedis.GetUserByToken(token, out UserInfo userInfo)) { return(Ok(new ApiResponse(userInfo))); } return(Ok(new ApiResponse(code: CodeAndMessage.UnKnownError))); }
[AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口 public IActionResult WordsUpLoad(WordDto body) { string token = _httpContext.HttpContext.Request.Headers["Authorization"]; AuthRedis.GetUserByToken(token, out UserInfo userInfo); using EFCoreContextWrite context = new EFCore.EFCoreContextWrite(); if (body.Eid == null) { int count = context.WordInfo.Where(x => x.PictureID == body.id).Count(); if (count > 0) { return(Ok(new ApiResponse(code: CodeAndMessage.已存在对应的资讯文档))); } WordInfo WordInfos = new WordInfo() { Id = SequenceID.GetSequenceID(), CreateTime = DateTime.Now, Disable = false, HtmlContent = body.HtmlContent, PictureID = body.id, LastModifiedTime = DateTime.Now, HtmlExplain = body.HtmlExplain, HtmlTitle = body.HtmlTitle, AttachedPath = body.AttachedPath }; context.Add(WordInfos); context.SaveChanges(); WordRedis.Del(); } else { var WordInfo = context.WordInfo.Single(x => x.Id == body.Eid); WordInfo.LastModifiedTime = DateTime.Now; WordInfo.HtmlContent = body.HtmlContent; WordInfo.HtmlExplain = body.HtmlExplain; WordInfo.HtmlTitle = body.HtmlTitle; WordInfo.AttachedPath = body.AttachedPath; context.SaveChanges(); WordRedis.Del(); } return(Ok(new ApiResponse())); }
/// <summary> /// 请求验证,当前验证部分不要抛出异常,ExceptionFilter不会处理 /// </summary> /// <param name="context"></param> public void OnAuthorization(AuthorizationFilterContext context) { string token = context.HttpContext.Request.Headers["Authorization"]; if (string.IsNullOrEmpty(token)) { context.Result = new StatusCodeResult((int)HttpStatusCode.Unauthorized); } if (!AuthRedis.GetUserByToken(token, out UserInfo userInfo)) { context.Result = new StatusCodeResult((int)HttpStatusCode.Unauthorized); } else { //注册账号时间不能超过俩个小时 if (DateTime.Now.Hour - userInfo.CreateTime.Hour > 2 && userInfo.LoginType.First() != LoginType.FreeWeb) { context.Result = new StatusCodeResult((int)HttpStatusCode.Unauthorized); } } }
//[AuthFilter]//身份认证,不带token或者token错误会被拦截器拦截进不来这个接口 public IActionResult PictureList() { using EFCoreContextWrite context = new EFCore.EFCoreContextWrite(); int total = 0; List <PictureListDto> PictureListDtos = new List <PictureListDto>(); if (!PictureRedis.GetAll(out List <PictureInfo> Picture)) { Picture = context.PictureInfo.Where(x => x.Disable == false).OrderByDescending(x => x.RecommendIndex).Include(x => x.Users).ToList(); total = Picture.Count(); if (Picture != null && Picture.Count > 0) { PictureRedis.SaveAll(Picture); } string token = _httpContext.HttpContext.Request.Headers["Authorization"]; if (string.IsNullOrEmpty(token)) { Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList(); } else { if (!AuthRedis.GetUserByToken(token, out UserInfo userInfo)) { Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList(); } else { //注册账号时间不能超过俩个小时 if (DateTime.Now.Hour - userInfo.CreateTime.Hour > 2 && userInfo.LoginType.First() != LoginType.FreeWeb) { Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList(); } } Picture = Picture.Where(x => x.Disable == false && x.PictureType == PictureType.News).ToList(); } foreach (var item in Picture) { PictureListDto PictureListDto = new PictureListDto() { ID = item.Id, CreateTime = item.CreateTime, Index = item.RecommendIndex, UserName = item.Users.UserName, PictureExplain = item.PictureExplain, PictureTitle = item.PictureTitle, PictureUrl = item.PictureContent, PictureType = item.PictureType }; PictureListDtos.Add(PictureListDto); } } else { total = Picture.Count(); string token = _httpContext.HttpContext.Request.Headers["Authorization"]; if (string.IsNullOrEmpty(token)) { Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList(); } else { if (!AuthRedis.GetUserByToken(token, out UserInfo userInfo)) { Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList(); } else { //注册账号时间不能超过俩个小时 if (DateTime.Now.Hour - userInfo.CreateTime.Hour > 2 && userInfo.LoginType.First() != LoginType.FreeWeb) { Picture = Picture.Where(x => x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).Skip(0).Take(4).ToList(); } } Picture = Picture.Where(x => x.Disable == false && x.PictureType == PictureType.News).OrderByDescending(x => x.RecommendIndex).ToList(); } foreach (var item in Picture) { PictureListDto PictureListDto = new PictureListDto() { ID = item.Id, CreateTime = item.CreateTime, Index = item.RecommendIndex, UserName = item.Users.UserName, PictureExplain = item.PictureExplain, PictureTitle = item.PictureTitle, PictureUrl = item.PictureContent, PictureType = item.PictureType }; PictureListDtos.Add(PictureListDto); } } return(Ok(new ApiResponse(PictureListDtos, total))); }