///<summary> /// Class AuthorizationFilter ///</summary> ///<seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAsyncAuthorizationFilter" /> publicclassAuthorizationFilter : IAsyncAuthorizationFilter { ///<summary> /// Called early in the filter pipeline to confirm request is authorized. ///</summary> ///<param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext" />.</param> publicasync Task OnAuthorizationAsync(AuthorizationFilterContext context) { var cookies = context.HttpContext.Request.Cookies;
cookies.TryGetValue("token", outstring token);
if (token.Equals("123456")) { var response = new FailResultViewModel { CorrelationId = Guid.NewGuid().ToString(), Method = $"{context.HttpContext.Request.Path}.{context.HttpContext.Request.Method}", Status = "UnAuthorized", Version = "1.0", Error = new FailInformation() { Domain = "ProjectName", Message = "未授權", Description = "授權驗證失敗" } };
///<summary> /// Class CacheResourceFilter ///</summary> ///<seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAsyncResourceFilter" /> publicclassCacheResourceFilter : IAsyncResourceFilter { privatestaticreadonly Dictionary<string, ObjectResult> _cache = new Dictionary<string, ObjectResult>();
///<summary> /// Called asynchronously before the rest of the pipeline. ///</summary> ///<param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ResourceExecutingContext" />.</param> ///<param name="next">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ResourceExecutionDelegate" />. Invoked to execute the next resource filter or the remainder /// of the pipeline.</param> publicasync Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { var cacheKey = context.HttpContext.Request.Path.ToString();
if (_cache != null && _cache.ContainsKey(cacheKey)) { var cacheValue = _cache[cacheKey]; if (cacheValue != null) { context.Result = cacheValue; } } else { var executedContext = await next();
var result = executedContext.Result as ObjectResult; if (result != null) { _cache.Add(cacheKey, result); } } } }
///<summary> /// Called asynchronously before the action, after model binding is complete. ///</summary> ///<param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext" />.</param> ///<param name="next">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutionDelegate" />. Invoked to execute the next action filter or the action itself.</param> publicasync Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var parameter = context.ActionArguments.SingleOrDefault(); if (parameter.Value isnull) { var response = new FailResultViewModel { CorrelationId = Guid.NewGuid().ToString(), Method = $"{context.HttpContext.Request.Path}.{context.HttpContext.Request.Method}", Status = "Error", Version = "1.0", Error = new FailInformation { Domain = "ProjectName", Message = "參數驗證失敗", Description = "傳入參數為null" } };
///<summary> /// Called asynchronously before the action, after model binding is complete. ///</summary> ///<param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext" />.</param> ///<param name="next">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutionDelegate" />. Invoked to execute the next action filter or the action itself.</param> publicasync Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var executedContext = await next();
var result = executedContext.Result as ObjectResult;
// ModelStateDictionary存放錯誤訊息 if (result != null && !(result.Value is HttpResponseMessage) && !(result.Value is SuccessResultViewModel<object>) && !(result.Value is FailResultViewModel) && !(result.Value is SuccessResultViewModel<ModelStateDictionary>)) { var responseModel = new SuccessResultViewModel<object> { Version = "1.0", Method = $"{context.HttpContext.Request.Path}.{context.HttpContext.Request.Method}", Status = "Success", CorrelationId = Guid.NewGuid().ToString(), Data = result.Value };
///<summary> /// Class ResultFilter ///</summary> ///<seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAsyncResultFilter" /> publicclassResultFilter : IAsyncResultFilter { ///<summary> /// Called asynchronously before the action result. ///</summary> ///<param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ResultExecutingContext" />.</param> ///<param name="next">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ResultExecutionDelegate" />. Invoked to execute the next result filter or the result itself.</param> publicasync Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { if (!(context.Result is EmptyResult)) { var headerName = "OnResultExecuting";
var headerValue = newstring[] { "ResultExecuting Successfully" };