protected virtual HttpResponseMessage CreateUnAuthorizedResponse(HttpActionContext actionContext)
        {
            HttpStatusCode statusCode;
            ErrorInfo error;

            if (actionContext.RequestContext.Principal?.Identity?.IsAuthenticated ?? false)
            {
                statusCode = HttpStatusCode.Forbidden;
                error = new ErrorInfo(
                    _localizationManager.GetString(AbpWebConsts.LocalizaionSourceName, "DefaultError403"),
                    _localizationManager.GetString(AbpWebConsts.LocalizaionSourceName, "DefaultErrorDetail403")
                );
            }
            else
            {
                statusCode = HttpStatusCode.Unauthorized;
                error = new ErrorInfo(
                    _localizationManager.GetString(AbpWebConsts.LocalizaionSourceName, "DefaultError401"),
                    _localizationManager.GetString(AbpWebConsts.LocalizaionSourceName, "DefaultErrorDetail401")
                );
            }

            var response = new HttpResponseMessage(statusCode)
            {
                Content = new ObjectContent<AjaxResponse>(
                    new AjaxResponse(error, true),
                    _configuration.HttpConfiguration.Formatters.JsonFormatter
                )
            };

            return response;
        }
        private static ErrorInfo CreateDetailedErrorInfoFromException(Exception exception)
        {
            var detailBuilder = new StringBuilder();

            AddExceptionToDetails(exception, detailBuilder);

            var errorInfo = new ErrorInfo(exception.Message, detailBuilder.ToString());

            if (exception is AbpValidationException)
            {
                errorInfo.ValidationErrors = GetValidationErrorInfos(exception as AbpValidationException);
            }

            return errorInfo;
        }
        public static MvcAjaxResponse ToMvcAjaxResponse(this ModelStateDictionary modelState)
        {
            if (modelState.IsValid)
            {
                return new MvcAjaxResponse();
            }

            var validationErrors = new List<ValidationErrorInfo>();

            foreach (var state in modelState)
            {
                foreach (var error in state.Value.Errors)
                {
                    validationErrors.Add(new ValidationErrorInfo(error.ErrorMessage, state.Key));
                }
            }

            var errorInfo = new ErrorInfo(AbpWebLocalizedMessages.ValidationError)
                            {
                                ValidationErrors = validationErrors.ToArray()
                            };

            return new MvcAjaxResponse(errorInfo);
        }
        public static AjaxResponse ToAjaxResponse(this ModelStateDictionary modelState)
        {
            if (modelState.IsValid)
            {
                return new AjaxResponse();
            }

            var validationErrors = new List<ValidationErrorInfo>();

            foreach (var state in modelState)
            {
                foreach (var error in state.Value.Errors)
                {
                    validationErrors.Add(new ValidationErrorInfo(error.ErrorMessage, state.Key));
                }
            }

            var errorInfo = new ErrorInfo(SingletonDependency<ILocalizationManager>.Instance.GetString(AbpWebConsts.LocalizaionSourceName, "ValidationError"))
            {
                ValidationErrors = validationErrors.ToArray()
            };

            return new AjaxResponse(errorInfo);
        }
Ejemplo n.º 5
0
        public static MvcAjaxResponse ToMvcAjaxResponse(ModelStateDictionary modelState)
        {
            if (modelState.IsValid)
            {
                return new MvcAjaxResponse();
            }

            var validationErrors = new List<ValidationErrorInfo>();

            foreach (var state in modelState)
            {
                foreach (var error in state.Value.Errors)
                {
                    validationErrors.Add(new ValidationErrorInfo(error.ErrorMessage, state.Key));
                }
            }

            var errorInfo = new ErrorInfo("Your request is not valid!")
                            {
                                ValidationErrors = validationErrors.ToArray()
                            };

            return new MvcAjaxResponse(errorInfo);
        }
        /// <summary>
        /// Creates an <see cref="MvcAjaxResponse"/> object with <see cref="AjaxResponse.Error"/> specified.
        /// <see cref="AjaxResponse.Success"/> is set as false.
        /// </summary>
        /// <param name="error">Error details</param>
        /// <param name="unAuthorizedRequest">Used to indicate that the current user has no privilege to perform this request</param>
        public MvcAjaxResponse(ErrorInfo error, bool unAuthorizedRequest = false)
            : base(error, unAuthorizedRequest)
        {

        }
Ejemplo n.º 7
0
 public ErrorViewModel(ErrorInfo errorInfo, Exception exception = null)
 {
     ErrorInfo = errorInfo;
     Exception = exception;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates an <see cref="AjaxResponse"/> object with <see cref="Error"/> specified.
 /// <see cref="Success"/> is set as false.
 /// </summary>
 /// <param name="error">Error details</param>
 /// <param name="unAuthorizedRequest">Used to indicate that the current user has no privilege to perform this request</param>
 public AjaxResponse(ErrorInfo error, bool unAuthorizedRequest = false)
 {
     Error = error;
     UnAuthorizedRequest = unAuthorizedRequest;
     Success = false;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a new instance of <see cref="ErrorInfo"/>.
 /// </summary>
 /// <param name="instanceToClone">Instance to clone</param>
 /// <param name="forAjax">Indicates whether the resultant instance is to be returned in AJAX response</param>
 public ErrorInfo(ErrorInfo instanceToClone, bool forAjax = false)
     : this(instanceToClone.Code, instanceToClone.Message, instanceToClone.Details)
 {
     ValidationErrors =
         (instanceToClone.ValidationErrors != null)
         ? instanceToClone.ValidationErrors.Select(ve => new ValidationErrorInfo(ve, forAjax: forAjax)).ToArray()
         : null;
 }
Ejemplo n.º 10
0
        private ErrorInfo CreateErrorInfoForFailedLoginAttempt(AbpLoginResultType result, string usernameOrEmailAddress)
        {
            ErrorInfo info = new ErrorInfo(500);
            
            switch (result)
            {
                case AbpLoginResultType.Success:
                    throw new ApplicationException("Don't call this method with a success result!");
                case AbpLoginResultType.InvalidUserNameOrEmailAddress:
                case AbpLoginResultType.InvalidPassword:
                    info.Message = L("LoginFailed");
                    info.Details = L("InvalidUserNameOrPassword");
                    break;

                case AbpLoginResultType.UserIsNotActive:
                    info.Message = L("LoginFailed");
                    info.Details = L("UserIsNotActiveAndCanNotLogin", usernameOrEmailAddress);
                    break;

                case AbpLoginResultType.UserEmailIsNotConfirmed:
                    info.Message = L("LoginFailed");
                    info.Details = "Your email address is not confirmed. You can not login";
                    break;
                    
                default: //Can not fall to default actually. But other result types can be added in the future and we may forget to handle it
                    Logger.Warn("Unhandled login fail reason: " + result);
                    
                    info.Message = L("LoginFailed");
                    info.Details = "Unhandled login fail reason: " + result;
                    break;
            }

            return info;
        }
Ejemplo n.º 11
0
        //[ValidateAntiForgeryToken]
        public async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "")
        {
            try
            {
                CheckModelState();

                var loginResult = await GetLoginResultAsync(
                    loginModel.UsernameOrEmailAddress,
                    loginModel.Password,
                    loginModel.TenancyName
                    );

                await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe);

                if (string.IsNullOrWhiteSpace(returnUrl))
                {
                    returnUrl = Request.ApplicationPath;
                }

                if (!string.IsNullOrWhiteSpace(returnUrlHash))
                {
                    returnUrl = returnUrl + returnUrlHash;
                }

                return Json(new AjaxResponse { TargetUrl = returnUrl });
            }
            catch (UserFriendlyException ex)
            {
                var error = new ErrorInfo(ex.Message);
                return Json(new AjaxResponse { Success = false, Error = error });
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Creates a new <see cref="AbpException"/> object.
 /// </summary>
 /// <param name="errorInfo">Exception message</param>
 public AbpRemoteCallException(ErrorInfo errorInfo)
     : base(errorInfo.Message)
 {
     ErrorInfo = errorInfo;
 }