Skip to content

Instantly share code, notes, and snippets.

@timiles
timiles / MockWebProxyHelper.cs
Created November 15, 2012 15:51
MockWebProxyHelper - a wrapper around Fiddler Core to assist with unit testing
using System;
using System.Net;
// http://www.fiddler2.com/fiddler/Core/
using Fiddler;
public static class MockWebProxyHelper
{
public enum HttpMethods
{
GET, POST, PUT, Unknown
@timiles
timiles / AntiForgeryTokenFilterProvider.cs
Created December 19, 2012 15:28
AntiForgeryTokenFilterProvider, plus js to attach to jQuery ajax requests
using System.Collections.Generic;
using System.Web.Mvc;
public class AntiForgeryTokenFilterProvider : IFilterProvider
{
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
if (controllerContext.HttpContext.Request.HttpMethod.ToUpper() == "POST")
{
yield return new Filter(new ValidateAntiForgeryTokenAttribute(), FilterScope.Global, null);
@timiles
timiles / gist:4445456
Last active August 5, 2021 15:49
Mock HttpContext.Current.Request.Headers. This is ugly but it works. credit: http://bigjimindc.blogspot.co.uk/2007/07/ms-kb928365-aspnet-requestheadersadd.html
HttpContext.Current = new HttpContext(
new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
NameValueCollection headers = HttpContext.Current.Request.Headers;
Type t = headers.GetType();
const BindingFlags nonPublicInstanceMethod = BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance;
t.InvokeMember("MakeReadWrite", nonPublicInstanceMethod, null, headers, null);
t.InvokeMember("InvalidateCachedArrays", nonPublicInstanceMethod, null, headers, null);
@timiles
timiles / TableNameManager.cs
Created April 12, 2013 10:39
Table name manager for Azure TableStorage. This solves a problem where deleting and recreating a (very large) table takes too long to propagate through the cloud - Azure SDK tools believes the table does not exist, yet a conflict is raised on creation of a new table with the same name. Rather than waiting on a retry policy, this code enables off…
using System.Linq;
using Microsoft.Experience.CloudFx.Framework.Configuration;
using Microsoft.Experience.CloudFx.Framework.Storage;
using Microsoft.WindowsAzure.Storage.Table;
public static class TableNameManager
{
private static readonly StorageAccountInfo StorageInfo;
static TableNameManager()
@timiles
timiles / RequestLogger.cs
Last active June 7, 2019 19:12
RequestLogger
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web;
using Microsoft.WindowsAzure.ServiceRuntime;
@timiles
timiles / hidefromtimeline.js
Last active October 20, 2023 21:12
Hide from timeline: javascript to hide all displayed posts from your facebook timeline. Doesn't delete anything, only hides from timeline.
/*---------------------
INSTRUCTIONS:
1. open https://www.facebook.com/me
2. run script*
3. refresh if necessary, goto 2.
* to save as bookmarklet, create new bookmark in your browser of choice -
Name: "hide from timeline"
URL: "javascript: <content of gist below>"
-----------------------*/
@timiles
timiles / removetagfromphoto.js
Created May 30, 2013 20:54
Remove tag from photo: javascript to remove a tag of yourself from a photo. Doesn't delete anything, only removes tag.
/*---------------------
INSTRUCTIONS:
1. open a photo, ie https://www.facebook.com/photo.php?fbid=XXXXXX
2. run script*
* to save as bookmarklet, create new bookmark in your browser of choice -
Name: "remove tag from photo"
URL: "javascript: <content of gist below>"
-----------------------*/
@timiles
timiles / SequentialId.cs
Created June 21, 2013 12:45
Sequential ID generator, useful as eg status history RowKeys in Azure TableStorage. Query by PartitionKey for statuses in chronological order; if partition is large and you're mostly only interested in recent statuses, use desc=true, then query partition with .Take(count).
public static class SequentialId
{
public static string NewId(bool desc = false)
{
var ticks = desc ? DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks : DateTime.UtcNow.Ticks;
return string.Format("{0:D19}-{1}", ticks, Guid.NewGuid());
}
}
@timiles
timiles / XFrameOptionsHeaderFilterProvider.cs
Last active December 18, 2015 19:19
Add X-FRAME-OPTIONS: SAMEORIGIN header to response, if you wish to protect against potential clickjacking. (http://en.wikipedia.org/wiki/Clickjacking#X-Frame-Options)
// NOTE: latest MVC's @Html.AntiForgeryToken() also sets this header automatically, so this may no longer be needed.
// register in Global.asax.cs: FilterProviders.Providers.Add(new XFrameOptionsHeaderFilterProvider());
public class XFrameOptionsHeaderFilterProvider : IFilterProvider
{
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
yield return new Filter(new XFrameOptionsHeaderAttribute(), FilterScope.Global, null);
}
class XFrameOptionsHeaderAttribute : ActionFilterAttribute
{
@timiles
timiles / bypass server certificate validation check
Last active September 29, 2017 15:43
bypass server certificate validation check
System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;