private static ChallengeResult GetResult(string message, RequestDetails requestDetails, HttpStatusCode statusCode)
        {
            var problemDetails = new ProblemDetails
            {
                Instance = requestDetails.Path,
                Status   = (int)statusCode,
                Title    = "Authentication error. See details.",
                Detail   = message,
            };

            var res = new ChallengeResult()
            {
                ContentType       = "application/problem+json",
                TextToWriteOutput = JsonSerializer.Serialize(problemDetails),
                StatusCode        = (int)statusCode
            };

            return(res);
        }
        public static Task <ChallengeResult> ChallengeResponse(Exception ex, RequestDetails requestDetails)
        {
            var res = new ChallengeResult();

            switch (ex)
            {
            case null:
                return(Task.FromResult(res));

            case CustomAuthException exception:
                switch (exception.ErrorCode)
                {
                case (int)ConstantsAuthentication.AuthResult.InvalidUserOrPass:
                case (int)AuthenticationError.AuthenticationFailed:
                    res = GetResult(ex.Message, requestDetails, HttpStatusCode.Unauthorized);
                    break;

                case (int)ConstantsAuthentication.AuthResult.NotAllowed:
                    res = GetResult(ex.Message, requestDetails, HttpStatusCode.Forbidden);
                    break;

                case (int)AuthenticationError.MethodNotAllowed:
                    res = GetResult(ex.Message, requestDetails, HttpStatusCode.MethodNotAllowed);
                    break;

                case (int)AuthenticationError.OtherError:
                    throw exception;

                default:
                    res.TextToWriteOutput = exception.Message;
                    break;
                }

                break;

            default:
                res.TextToWriteOutput = ex.Message;
                break;
            }

            return(Task.FromResult(res));
        }