public void OnActionExecuting(ActionExecutingContext filterContext) { var messages = Convert.ToString(_httpContextAccessor.HttpContext.Request.Cookies[CookiePrefix]); if (String.IsNullOrEmpty(messages)) { return; } IList<NotifyEntry> messageEntries; messageEntries = DeserializeNotifyEntries(messages); if(messageEntries == null) { // An error occured during deserialization _shouldDeleteCookie = true; return; } if (!messageEntries.Any()) { return; } // Make the notifications available for the rest of the current request. _existingEntries = messageEntries; }
public override void OnActionExecuting(ActionExecutingContext actionContext) { if (!actionContext.ModelState.IsValid) { actionContext.Result = new BadRequestObjectResult(actionContext.ModelState); } }
public override void OnActionExecuting(ActionExecutingContext context) { if (!context.RouteData.DataTokens.ContainsKey("actionName")) { context.RouteData.DataTokens.Add("actionName", ((ControllerActionDescriptor)context.ActionDescriptor).ActionName); } }
public override void OnActionExecuting(ActionExecutingContext actionContext) { if (!actionContext.HttpContext.Request.Cookies.ContainsKey(Constants.Infozonare)) { actionContext.Result = new BadRequestObjectResult("You don't work here. Take a beer and party on!"); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!ShouldSaveAudit(context)) { await next(); return; } var auditInfo = CreateAuditInfo(context); var stopwatch = Stopwatch.StartNew(); try { await next(); } catch (Exception ex) { auditInfo.Exception = ex; throw; } finally { stopwatch.Stop(); auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); AuditInfoProvider?.Fill(auditInfo); await AuditingStore.SaveAsync(auditInfo); } }
public void OnActionExecuting_ChangesActionResult_IfUnsupportedContentTypeExceptionIsFoundOnModelState() { // Arrange var context = new ActionExecutingContext( new ActionContext { HttpContext = new DefaultHttpContext(), RouteData = new RouteData(), ActionDescriptor = new ActionDescriptor() }, new List<IFilterMetadata>(), new Dictionary<string, object>(), new object()); var modelMetadata = new EmptyModelMetadataProvider() .GetMetadataForType(typeof(int)); context.ModelState.AddModelError( "person.body", new UnsupportedContentTypeException("error"), modelMetadata); var filter = new UnsupportedContentTypeFilter(); // Act filter.OnActionExecuting(context); // Assert Assert.NotNull(context.Result); var status = Assert.IsType<UnsupportedMediaTypeResult>(context.Result); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { using (AbpCrossCuttingConcerns.Applying(context.Controller, AbpCrossCuttingConcerns.Auditing)) { if (!ShouldSaveAudit(context)) { await next(); return; } var auditInfo = CreateAuditInfo(context); var stopwatch = Stopwatch.StartNew(); try { var result = await next(); if (result.Exception != null && !result.ExceptionHandled) { auditInfo.Exception = result.Exception; } } catch (Exception ex) { auditInfo.Exception = ex; throw; } finally { stopwatch.Stop(); auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); AuditInfoProvider?.Fill(auditInfo); await AuditingStore.SaveAsync(auditInfo); } } }
public override void OnActionExecuting(ActionExecutingContext context) { if (!IsMultipartContentType(context.HttpContext.Request.ContentType)) { context.Result = new StatusCodeResult(415); return; } base.OnActionExecuting(context); }
public void OnActionExecuting(ActionExecutingContext context) { MiniProfiler.Start(); if (MiniProfiler.Current != null) { Stack<IDisposable> stack; if (context.ActionDescriptor.Properties.ContainsKey(StackKey)) { stack = context.ActionDescriptor.Properties[StackKey] as Stack<IDisposable>; } else { stack = new Stack<IDisposable>(); context.ActionDescriptor.Properties[StackKey] = (object)stack; } MiniProfiler current = MiniProfiler.Current; if (current != null) { string controllerName = context.Controller.GetType().Name; string actionName = context.ActionDescriptor.DisplayName; stack.Push(MiniProfilerExtensions.Step(current, "Controller: " + controllerName + actionName)); } } }
/// <summary> /// Called when [action executing]. /// </summary> /// <param name="actionContext">The action context.</param> public override void OnActionExecuting(ActionExecutingContext actionContext) { if (actionContext.ActionArguments.Any(p => p.Value == null)) { actionContext.Result = new BadRequestObjectResult("Arguments cannot be null."); } }
public override void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { var controller = context.Controller as Controller; if (controller != null) { if (IsAjaxRequest(context.HttpContext.Request)) { // Send 400 status code (Bad Request). context.Result = new BadRequestObjectResult(SerialiseModelState(context.ModelState)); } else { controller.TempData[Key] = SerialiseModelState(context.ModelState); // Redirect back to GET action. context.Result = new RedirectToRouteResult(context.RouteData.Values); } } } base.OnActionExecuting(context); }
/// <inheritdoc /> public void OnActionExecuting(ActionExecutingContext context) { if (HasUnsupportedContentTypeError(context)) { context.Result = new UnsupportedMediaTypeResult(); } }
public override void OnActionExecuting(ActionExecutingContext context) { var configuration = context.HttpContext.RequestServices.GetService<IConfiguration>(); ViewBag.Keywords = configuration["SiteKeywords"]; ViewBag.Description = configuration["SiteDescription"]; base.OnActionExecuting(context); }
public async Task OnActionExecutionAsync( ActionExecutingContext context, ActionExecutionDelegate next) { // do something before the action executes await next(); // do something after the action executes }
public void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { context.Result = new BadRequestObjectResult(new ApiError { Message = "The model is invalid." }); } }
public override void OnActionExecuting(ActionExecutingContext actionContext) { //actionContext.HttpContext.Request.Body if (actionContext.ModelState.IsValid == false) { actionContext.Result = new BadRequestObjectResult(actionContext.ModelState); } }
public override void OnActionExecuting(ActionExecutingContext context) { context.Result = new ContentResult { Content = "The Action was never executed", ContentType = "text/plain" }; }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var user = filterContext.HttpContext.User; if (!user.IsInRole("Admin")) { filterContext.Result = new RedirectToActionResult("index", "home", null); base.OnActionExecuting(filterContext); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Result = new RedirectToActionResult("index", "home", null); } base.OnActionExecuting(filterContext); }
private void SetModelStateToInvalid(ActionExecutingContext filterContext) { var modelState = ((Controller)filterContext.Controller).ViewData.ModelState; if (!modelState.ContainsKey(ModelStateErrorKey)) { modelState.AddModelError(ModelStateErrorKey, "Verification failed"); } }
public void OnActionExecuting(ActionExecutingContext context) { var controller = context.Controller as Controller; if (controller != null) { var modelBinderAccessor = context.HttpContext.RequestServices.GetRequiredService<IModelUpdaterAccessor>(); modelBinderAccessor.ModelUpdater = new ControllerModelUpdater(controller); } }
public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); if (!context.ModelState.IsValid) { context.Result = new BadRequestObjectResult(context.ModelState); } }
private bool ShouldSaveAudit(ActionExecutingContext actionContext) { if (!_auditingConfiguration.IsEnabled || !_auditingConfiguration.MvcControllers.IsEnabled) { return false; } return _auditingHelper.ShouldSaveAudit(actionContext.ActionDescriptor.GetMethodInfo(), true); }
public void Initialize(ActionExecutingContext actionContext) { base.Initialize( actionContext.ActionDescriptor.GetMethodInfo(), GetParameterValues(actionContext) ); ActionContext = actionContext; }
/// <summary> /// check if request is authorized /// </summary> /// <param name="context"></param> /// <param name="roles"></param> /// <returns></returns> private bool IsAuthorized(ActionExecutingContext context, string[] roles) { var principal = context.HttpContext.User; if (principal == null) return false; var identity = principal.Identity as ErpIdentity; return identity != null; }
public override void OnActionExecuting(ActionExecutingContext context) { var controllerActionDescriptor = (ControllerActionDescriptor)context.ActionDescriptor; if (controllerActionDescriptor.MethodInfo == typeof(ActionFilterController).GetMethod(nameof(ActionFilterController.GetHelloWorld))) { (context.ActionArguments["fromGlobalActionFilter"] as List<ContentResult>). Add(Helpers.GetContentResult(context.Result, "Action Filter - OnActionExecuting")); } }
public override void OnActionExecuting(ActionExecutingContext context) { var c = context.Controller as BaseController; if (c != null) { c.ViewBag.AppBaseUrl = c.AppBaseUrl; } base.OnActionExecuting(context); }
/// <summary> /// Returns <c>true</c> if the controller name starts with Admin or if the <see cref="AdminAttribute"/> /// is applied to the controller or the action. /// </summary> private static bool IsAdmin(ActionExecutingContext context) { // Does the controller start with "Admin" if (IsNameAdmin(context)) { return true; } return false; }
public void OnActionExecuting(ActionExecutingContext context) { Apply(context.HttpContext); // TODO: Check permission //if (!_authorizer.Authorize(StandardPermissions.AccessAdminPanel, T("Can't access the admin"))) //{ // filterContext.Result = new UnauthorizedResult(); //} }
public void OnActionExecuting(ActionExecutingContext context) { if (context.ActionDescriptor.DisplayName == "FiltersSample.Controllers.HomeController.Hello") { // Manipulating action arguments... if (!context.ActionArguments.ContainsKey("name")) { context.ActionArguments["name"] = "Steve"; } } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var ctx = _contextFactory.Create(context); if (ctx.IsModelValid == false) { var badRequestResult = GetBadRequestResult(ctx); ctx.SetResult(badRequestResult); return; } await next(); }
public async Task OnActionExecutionAsync(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext context, ActionExecutionDelegate next) { var session = ExtractJwtSessionFilterAttribute.FromItems(context.HttpContext); if (session == null || session.Expires < DateTime.Now) { context.Result = new UnauthorizedResult(); return; } ((BaseController)context.Controller).Session = session; await next.Invoke(); }
private static ActionExecutingContext ConstractActionExecutingContext() { var actionContext = new ActionContext( new Mock <HttpContext>().Object, new RouteData(), new Mock <ControllerActionDescriptor>().Object, new Mock <ModelStateDictionary>().Object); var actionExecutedContext = new ActionExecutingContext( actionContext, new List <IFilterMetadata>(), new Dictionary <string, object>(), new object()); return(actionExecutedContext); }
public override void OnActionExecuting(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext context) { RequestQuery = GetQuery(); if (HttpContext.Session.Keys.Count() == 0) { if (HttpContext.User.Identity != null && HttpContext.User.Identity.IsAuthenticated) { var cl = HttpContext.User.Claims.Where(c => c.Type == ClaimTypes.Sid); if (cl.Count() > 0) { (new Web.AuthService()).PrepareUserSession(int.Parse(cl.First().Value), HttpContext); if (HttpContext.Session.IsAvailable) { WebAppContext = (new Web.AuthService()).GetAppContext(_appSetting, HttpContext); WebAppContext.AppRoot = Url.Content("~/"); WebAppContext.RequestQuery = RequestQuery; } } else { (new Web.AuthService()).Logout(HttpContext); context.Result = RedirectToAction("Login", "Auth", new { isLogout = true }); } } else { (new Web.AuthService()).Logout(HttpContext); context.Result = RedirectToAction("Login", "Auth", new { isLogout = true }); } } else { if (HttpContext.Session.IsAvailable) { WebAppContext = (new Web.AuthService()).GetAppContext(_appSetting, HttpContext); WebAppContext.AppRoot = Url.Content("~/"); WebAppContext.RequestQuery = RequestQuery; } WebAppContext.RequestQuery = RequestQuery; } }
/// <summary> /// Called when action is executed /// </summary> /// <param name="context">context of executed action</param> public override void OnActionExecuting(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext context) { base.OnActionExecuting(context); }
public void OnActionExecuting(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext context) { }
public Task OnActionExecutionAsync(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext context, Microsoft.AspNetCore.Mvc.Filters.ActionExecutionDelegate next) { //do work return(next()); }
public void OnActionExecuting(ActionExecutingContext context) { }