Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
LSTANCZYK / RequireApiKey
Created March 21, 2017 16:55 — forked from RhysC/RequireApiKey
RequireApiKey require an api key for MVC controllers assumes SSL
public class RequireApiKey : ActionFilterAttribute
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(RequireApiKey));
public override void OnActionExecuting(HttpActionContext context)
{
var ipAddress = GetIpAddress(context);
Logger.InfoFormat("API attempt. Uri {0} - IP {1} - Headers {2} ", context.Request.RequestUri, ipAddress, context.Request.Headers);
IEnumerable<string> values;
if (context.Request.Headers.TryGetValues("ApiKey", out values) && GetApiKeys().Any (x => x ==values.First())
@LSTANCZYK
LSTANCZYK / CheckApplicationOfflineAttribute.cs
Created March 21, 2017 16:55 — forked from danielgreen/CheckApplicationOfflineAttribute.cs
A filter (which should be used as a global filter) to check whether the application is configured as offline, and throw an exception if this is the case. This is an alternative to placing a file named App_Offline.htm in the application's root. Using App_Offline.htm affects all users, cannot be selectively bypassed as in this case, and does not a…
using System;
using System.Net;
using System.Web.Mvc;
using System.Web.Routing;
namespace Web.Filters
{
/// <summary>
/// Check whether the application is marked as offline in the config file.
/// If so, and the user does not have the override permission, throw an exception.
@LSTANCZYK
LSTANCZYK / MinifyInlineScriptsAttribute.cs
Created March 21, 2017 16:55 — forked from raducugheorghe/MinifyInlineScriptsAttribute.cs
[C#][MVC] Minify inline scripts and styles
public class MinifyInlineScriptsAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var originalFilter = filterContext.HttpContext.Response.Filter;
if(originalFilter != null) filterContext.HttpContext.Response.Filter = new MinifyStream(originalFilter);
base.OnActionExecuting(filterContext);
}
@LSTANCZYK
LSTANCZYK / ThrottleRequestWebApi.cs
Created March 21, 2017 16:55 — forked from praveensewak/ThrottleRequestWebApi.cs
How to throttle requests in a Web Api?
[Route("api/dothis/{id}")]
[AcceptVerbs("POST")]
[Throttle(Name = "ApiThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
[Authorize]
public HttpResponseMessage DoThis(int id)
{
// do something
}
public class ThrottleAttribute : ActionFilterAttribute
@LSTANCZYK
LSTANCZYK / AuditLogActionFilter.cs
Created March 21, 2017 16:55 — forked from guntidheerajkumar/AuditLogActionFilter.cs
AuditLogActionFilter Class File
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace SampleAudit
{
@LSTANCZYK
LSTANCZYK / DeviceTypeRedirect.cs
Created March 21, 2017 16:55 — forked from kevinblake/DeviceTypeRedirect.cs
MVC4 action filter attribute to redirect to a different controller if a page is not valid for mobile/non-mobile context
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.WebPages;
namespace MyNamespace
{
public enum DeviceType {Mobile, Desktop}
public class DeviceTypeRedirect : ActionFilterAttribute
{
using System.Web.Mvc;
namespace MvcApplication1.Models
{
public class CheckBrowserAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
@LSTANCZYK
LSTANCZYK / WordDocumentAttribute.cs
Created March 21, 2017 16:55 — forked from bjcull/WordDocumentAttribute.cs
A Filter Attribute that lets you download an ASP.NET MVC View as a Word Document
public class WordDocumentAttribute : ActionFilterAttribute
{
public string DefaultFilename { get; set; }
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var result = filterContext.Result as ViewResult;
if (result != null)
result.MasterName = "~/Views/Shared/_LayoutWord.cshtml";
@LSTANCZYK
LSTANCZYK / LogActionWebApiFilter.cs
Created March 21, 2017 16:55 — forked from davidbreyer/LogActionWebApiFilter.cs
Custom Web API Filter with Log4net logging.
public class LogActionWebApiFilter : ActionFilterAttribute
{
/// <summary>
/// Instance of the Log4Net log.
/// </summary>
[Dependency] //Part 1
public ILog Log { get; set; }
//This function will execute before the web api controller
//Part 2
using System;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http.Filters;
using Newtonsoft.Json.Serialization;
namespace Filters
{
[AttributeUsage(AttributeTargets.All)]
public class CamelCasingFilterAttribute : ActionFilterAttribute