コード例 #1
0
 /// <summary>
 /// Invoked if Authorization fails and results in a Forbidden response
 /// </summary>
 /// <returns></returns>
 public static Func <ForbiddenContext, Task> OnForbiddenHandler()
 {
     return(context =>
     {
         var response = ResultObject.Error("Unauthorized.", "OnForbiddenHandler", code: ResultCode.ErrorAuthenticationFail);
         context.HttpContext.Response.ContentType = "application/json; charset=utf-8";
         context.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
         context.HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(response)).Wait();
         return Task.CompletedTask;
     });
 }
コード例 #2
0
        public void OnException(ExceptionContext context)
        {
            string errorMessage = context.Exception.ToString();
            var    response     = ResultObject.Error(context.Exception.Message, errorMessage, code: ResultCode.ErrorException);

            context.Result = new ContentResult()
            {
                Content     = JsonConvert.SerializeObject(response),
                ContentType = "application/json; charset=utf-8",
                StatusCode  = (int)HttpStatusCode.OK
            };
            context.ExceptionHandled = true;
        }
コード例 #3
0
        /// <summary>
        /// Invoked before a challenge is sent back to the caller.
        /// </summary>
        /// <returns></returns>
        public static Func <JwtBearerChallengeContext, Task> OnChallengeHandler()
        {
            return(context =>
            {
                context.HandleResponse();
                var response = ResultObject.Error("Unauthorized.",
                                                  context.AuthenticateFailure?.ToString() ?? string.Empty,
                                                  code: ResultCode.ErrorAuthenticationFail);

                if (context.AuthenticateFailure?.GetType() == typeof(SecurityTokenExpiredException))
                {
                    response.Code = ResultCode.ErrorTokenExpired;
                    response.Message.Message = "Phiên làm việc của bạn đã kết thúc. Vui lòng đăng nhập lại.";
                }
                context.HttpContext.Response.ContentType = "application/json; charset=utf-8";
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
                context.HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(response)).Wait();
                return Task.CompletedTask;
            });
        }
コード例 #4
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.ModelState.IsValid)
            {
                return;
            }
            var validationErrors = string.Join(" | ", context.ModelState
                                               .Keys
                                               .SelectMany(k => context.ModelState[k].Errors)
                                               .Select(e => e.ErrorMessage)
                                               .ToArray());

            var response = ResultObject.Error(
                "Thông tin không hợp lệ! Vui lòng kiểm tra lại.",
                validationErrors,
                code: ResultCode.ErrorInputInvalid);

            context.HttpContext.Response.StatusCode = 200;
            context.Result = new ContentResult()
            {
                Content     = JsonConvert.SerializeObject(response),
                ContentType = "application/json; charset=utf-8"
            };
        }
コード例 #5
0
        public void OnException(ExceptionContext context)
        {
            string errorId = (Convert.ToBase64String(Guid.NewGuid().ToByteArray())).Replace("=", "");
            string message = $"Đã xảy ra lỗi trong quá trình xử lý ({errorId}).";

            LoggingHelper.SetProperty("ErrorId:", errorId);
            string errorMessage = context.Exception.ToString();

            // TODO
            if (context.Exception.GetType() == typeof(CustomValidationException))
            {
                var validationException = (CustomValidationException)context.Exception;
                message = string.Join(" | ", validationException.Errors.SelectMany(x => x.Value));
            }
            var response = ResultObject.Error(message, errorMessage, code: ResultCode.ErrorException);

            context.Result = new ContentResult()
            {
                Content     = JsonConvert.SerializeObject(response),
                ContentType = "application/json; charset=utf-8",
                StatusCode  = (int)HttpStatusCode.OK
            };
            context.ExceptionHandled = true;
        }