コード例 #1
0
        //private readonly ITableHandler<ApiErrorResponse> tableHandler;

        //public ApiErrorResponseRepository(ITableHandler<ApiErrorResponse> tableHandler)
        //{
        //    this.tableHandler = tableHandler;
        //}

        //public async Task<Response> Create(ApiErrorResponseModel apiErrorResponseModel)
        //{
        //    var apiErrorResponse = new ApiErrorResponse
        //    {
        //        ErrorCode = apiErrorResponseModel.ErrorCode,
        //        ReasonPhrase = apiErrorResponseModel.ReasonPhrase,
        //        RequestBody = apiErrorResponseModel.RequestBody,
        //        RequestUri = apiErrorResponseModel.RequestUri,
        //        ResponseBody = apiErrorResponseModel.ResponseBody
        //    };

        //    var result = await this.tableHandler.InsertAsync(apiErrorResponse);

        //    return result;
        //}



        //public ApiErrorResponseRepository(ITableHandler<ApiErrorResponse> tableHandler)
        //{
        //    this.tableHandler = tableHandler;
        //}

        public async Task <Response> Create(ApiErrorResponseModel apiErrorResponseModel)
        {
            //var apiErrorResponse = new ApiErrorResponse
            //{
            //    ErrorCode = apiErrorResponseModel.ErrorCode,
            //    ReasonPhrase = apiErrorResponseModel.ReasonPhrase,
            //    RequestBody = apiErrorResponseModel.RequestBody,
            //    RequestUri = apiErrorResponseModel.RequestUri,
            //    ResponseBody = apiErrorResponseModel.ResponseBody
            //};

            //var result = await this.tableHandler.InsertAsync(apiErrorResponse);

            //return result;

            return(null);
        }
コード例 #2
0
        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ClientApplicationAccessRequirement requirement)
        {
            var defaultMessage = "No Autorizado";

            var authHeader = _HttpContextAccessor.HttpContext.Request.Headers[HeaderNames.Authorization];

            if (!string.IsNullOrEmpty(authHeader) && authHeader.ToString().StartsWith("Basic"))
            {
                string accessKey = authHeader.ToString().Substring("Basic ".Length).Trim();

                _ApplicationProfile.ApplicationId = await _CacheService.GetCachedAsync(accessKey, async() =>
                {
                    var repo = _DataRepositoryFactory.GetDataRepository <ApplicationKey>();

                    var result = await repo.GetAsync(x => x.Select(r => r.ApplicationId), x => !x.IsDeleted && x.AccessKey == accessKey);

                    return(result);
                }, TimeSpan.FromMinutes(5));

                if (_ApplicationProfile.ApplicationId != null)
                {
                    context.Succeed(requirement);
                    return;
                }

                defaultMessage = "The provided Access Key is invalid.";
            }

            var responseModel = new ApiErrorResponseModel()
            {
                Message = defaultMessage
            };

            var result = JsonConvert.SerializeObject(responseModel);

            _HttpContextAccessor.HttpContext.Response.ContentType = MediaTypeNames.Application.Json;
            _HttpContextAccessor.HttpContext.Response.StatusCode  = (int)HttpStatusCode.Unauthorized;
            await _HttpContextAccessor.HttpContext.Response.WriteAsync(result);
        }
        private static Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            var    code        = HttpStatusCode.InternalServerError; // 500 if unexpected
            string messageCode = null;
            var    message     = exception.Message;

            var responseModel = new ApiErrorResponseModel();

            if (exception is SecurityException)
            {
                message = "Acceso no autorizado.";

                code = HttpStatusCode.Unauthorized;
            }
            else if (exception is AggregateException)
            {
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    message = "El PEI Solicitado no ha sido creado.";

                    code = HttpStatusCode.BadRequest;
                }
            }
            else if (exception is SecurityCustomException)
            {
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    message = exception.Message;
                }

                code = HttpStatusCode.Unauthorized;
            }
            else if (exception is ArgumentNullException)
            {
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    message = exception.Message;
                }
                else
                {
                    message = "Error en parámetros recibidos.";
                }

                code = HttpStatusCode.BadRequest;
            }
            else
            {
                switch (exception)
                {
                case ModelValidationException _:
                    if (string.IsNullOrEmpty(exception.Message))
                    {
                        message = "Error en parámetros recibidos.";
                    }

                    responseModel.Errors = ((ModelValidationException)exception).Errors;

                    code = HttpStatusCode.BadRequest;
                    break;

                case System.InvalidOperationException _:
                    if (!string.IsNullOrEmpty(exception.Message))
                    {
                        message = exception.Message;
                    }
                    else
                    {
                        message = "Operación inválida.";
                    }

                    code = HttpStatusCode.BadRequest;
                    break;

                case ArgumentException _:
                    if (!string.IsNullOrEmpty(exception.Message))
                    {
                        message = exception.Message;
                    }
                    else
                    {
                        message = "Error en parámetros recibidos.";
                    }

                    code = HttpStatusCode.BadRequest;
                    break;

                case ValidationException _:
                    if (!string.IsNullOrEmpty(exception.Message))
                    {
                        message = exception.Message;
                    }
                    else
                    {
                        message = "Operación inválida.";
                    }

                    messageCode = ((ValidationException)exception).Code;
                    code        = HttpStatusCode.BadRequest;
                    break;

                case NotFoundException _:
                    if (!string.IsNullOrEmpty(exception.Message))
                    {
                        message = exception.Message;
                    }

                    code = HttpStatusCode.NotFound;

                    Log.Error(exception, exception.Message, exception);
                    break;

                case ProfileNotFoundException _:
                    if (!string.IsNullOrEmpty(exception.Message))
                    {
                        message = exception.Message;
                    }

                    code = HttpStatusCode.BadRequest;

                    Log.Error(exception, exception.Message, exception);
                    break;

                case UriFormatException _:
                    message = "Formato de ruta inválido.";

                    code = HttpStatusCode.BadRequest;

                    Log.Error(exception, exception.Message, exception);
                    break;

                case HttpRequestException _:
                    message = "Error de comunicación.";

                    code = HttpStatusCode.BadRequest;

                    Log.Error(exception, exception.Message, exception);
                    break;

                case EntityValidationException _:
                    code = HttpStatusCode.InternalServerError;
                    break;

                case StorageException _:
                case DatabaseException _:
                    code = HttpStatusCode.InternalServerError;

                    Log.Error(exception, exception.Message, exception);
                    break;

                default:
                    Log.Error(exception, exception.Message, exception);
                    break;
                }
            }

            responseModel.MessageCode = messageCode;
            responseModel.Message     = message;

            var _errors = new Dictionary <string, IEnumerable <string> >();

            _errors.Add("errors", new List <string>()
            {
                message
            });

            responseModel.Errors = _errors;

#if DEBUG
            Debug.WriteLine($"Message: {exception.Message}, StackTrace: {exception.StackTrace}");
#endif

            var result = JsonConvert.SerializeObject(responseModel);
            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;
            return(context.Response.WriteAsync(result));
        }