Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
LSTANCZYK / AppInsightsFilter.cs
Created March 21, 2017 17:02 — forked from debugthings/AppInsightsFilter.cs
Enable Application Insights on Migraged Azure Mobile Services applications
internal static class RequestTrackingConstants
{
/// <summary>
/// Name of the HttpContext item containing RequestTelemetry object.
/// </summary>
internal const string RequestTelemetryItemName = "Microsoft.ApplicationInsights.RequestTelemetry";
internal const string EndRequestCallFlag = "Microsoft.ApplicationInsights.EndRequestCallFlag";
/// <summary>
@LSTANCZYK
LSTANCZYK / CapturingResponseFilter.cs
Created March 21, 2017 17:02 — forked from allenp/CapturingResponseFilter.cs
Filter attribute to capture and log requests and responses in ASP.NET
class CapturingResponseFilter : Stream
{
private Stream _sink;
private MemoryStream mem;
public APIAccessLogItem AccessLogItem { get; set; }
public CapturingResponseFilter(Stream sink, APIAccessLogItem item)
{
_sink = sink;
@LSTANCZYK
LSTANCZYK / PdfFilterAttribute.cs
Created March 21, 2017 17:02 — forked from kmpm/PdfFilterAttribute.cs
Filter for an MVC4 application to render all views as PDF if AcceptTypes contains "application/pdf" or querystring contains asPDF=1. Uses wkthml2pdf via Pechkin. Currently requires https://github.com/gmanny/Pechkin/pull/42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;
using System.Web.UI;
using Pechkin;
using System.Text.RegularExpressions;
using System;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Caching;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
/// <summary>
/// Decorates any Action that needs to have client requests limited by concurrent requests.
[AttributeUsage(AttributeTargets.Method)]
public class ValidationActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
InnerOnActionExecuting(actionContext);
var modelState = actionContext.ModelState;
if (modelState.IsValid)
@LSTANCZYK
LSTANCZYK / RestrictByIpAddressAttribute.cs
Created March 21, 2017 17:01
Ip address restriction action filter
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Web.Attributes
{
public class RestrictByIpAddressAttribute : ActionFilterAttribute
using Auction.Web.Domain;
using Auction.Web.Domain.Commands;
using Auction.Web.Domain.Entities;
using Auction.Web.Domain.Queries;
using Facebook;
using Microsoft.Web.WebPages.OAuth;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
@LSTANCZYK
LSTANCZYK / BeforeFilterAttribute.cs
Created March 21, 2017 16:55 — forked from beccasaurus/BeforeFilterAttribute.cs
Rails-like before filter
public class BeforeAttribute : ActionFilterAttribute {
public BeforeAttribute() {
Methods = string.Empty;
Except = string.Empty;
Only = string.Empty;
}
public BeforeAttribute(params string[] methodNames) : this() {
Methods = string.Join(" ", methodNames);
}
@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.