public static JsonResultEx Create(object obj = null, HttpStatusCode?statusCode = null, string statusDescription = null) { var retVal = new JsonResultEx { StatusCode = statusCode, StatusDescription = statusDescription }; var modelStateDictionary = obj as ModelStateDictionary; if (modelStateDictionary != null && !modelStateDictionary.IsValid) { foreach (var modelError in modelStateDictionary.Values.SelectMany(modelState => modelState.Errors)) { retVal.AddError(modelError.ErrorMessage); } } var identityResult = obj as IdentityResult; if (identityResult != null && !identityResult.Succeeded) { foreach (var error in identityResult.Errors) { retVal.AddError(error); } } if (modelStateDictionary == null && identityResult == null) { retVal.Data = obj; } return(retVal); }
public void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) { throw new ArgumentNullException(nameof(filterContext)); } var headerToken = filterContext.HttpContext.Request.Headers["X-XSRF-Token"]; var cookieToken = filterContext.HttpContext.Request.Cookies[AntiForgeryConfig.CookieName]; try { AntiForgery.Validate(cookieToken?.Value, headerToken); } catch (HttpAntiForgeryException ex) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = JsonResultEx.Create(HttpStatusCode.BadRequest, ex.Message); } else { throw; } } }