Skip to content

Instantly share code, notes, and snippets.

View iamandycohen's full-sized avatar

Andy Cohen iamandycohen

View GitHub Profile
@iamandycohen
iamandycohen / Impersonation
Last active July 12, 2021 02:29
Impersonation
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security.Principal;
public class UserImpersonator : IDisposable
{
[DllImport("advapi32.dll", SetLastError = true)]
@iamandycohen
iamandycohen / gist:5020371
Created February 23, 2013 16:35
XmlUtility
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
@iamandycohen
iamandycohen / gist:5020382
Created February 23, 2013 16:39
Common Extensions
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
public static class CommonExtensions
{
public static string ToQueryString(this NameValueCollection nvc)
@iamandycohen
iamandycohen / gist:5020386
Created February 23, 2013 16:40
BackAction
using System.Web.Mvc;
public static class UrlExtensions
{
public static string BackAction(this UrlHelper urlHelper, string actionName)
{
string url = null;
var urlReferrer = urlHelper.RequestContext.HttpContext.Request.UrlReferrer;
if (urlReferrer != null)
{
@iamandycohen
iamandycohen / gist:5064893
Created March 1, 2013 14:11
JavaScript Timer
Timer = function (timerFn, delay, intervalFn, interval) {
/// <summary>Set a timer/timeout with Stop, Start, and Restart capability</summary>
/// <param name="timerFn" type="Function">Function to run after the amount of time specified by the delay parameter</param>
/// <param name="delay" type="Number">Amount of time to delay the timerFn</param>
/// <param name="intervalFn" type="Function">Function to run after the interval specified in the interval parameter</param>
/// <param name="interval" type="Number">Amount of time to run the intervalFn function (frequency)</param>
var _self = this,
_timerFn = timerFn,
_delay = delay,
@iamandycohen
iamandycohen / gist:5064903
Created March 1, 2013 14:12
JavaScript Inactivity Callback
/// <reference path="jquery-1.8.2.js" />
/// <reference path="Timer.js" />
InactivityPoll = function (inactiveFn, inactiveDelay, confirmStartedFn, confirmFn, confirmDelay, intervalFn, interval, debug) {
/// <param name="inactiveFn" type="Function">Function to call when the inactivityDelay time has been passed</param>
/// <param name="inactiveDelay" type="Number">Amount of time to wait before calling the inactiveFn</param>
/// <param name="confirmStartedFn" type="Function">Function to call when the when the confirmDely starts counting down</param>
/// <param name="confirmFn" type="Function">Function to call when the confirmDelay time has been passed</param>
/// <param name="confirmDelay" type="Number">Amount of time to wait before calling the confirmFn</param>
/// <param name="intervalFn" type="Function">Function to run after the interval specified in the interval parameter</param>
@iamandycohen
iamandycohen / gist:5064918
Created March 1, 2013 14:15
Usage of InactivityTimer
var $saveAndExit = $('#saveAndExit'),
$inactivity = $('#inactivity'),
$saveAndExitRedirectSpan = $saveAndExit.find('.redirectTime'),
saveAndExitRedirectTime = $saveAndExit.data('redirecttime'),
saveAndExitRedirectExecutesAt = new Date().AddMilliseconds(parseInt(saveAndExitRedirectTime)),
inactivityInactivityTime = $inactivity.data('inactivitytime'),
inactivityRedirectTime = $inactivity.data('redirecttime'),
$inactivityRedirectSpan = $inactivity.find('.redirectTime');
/// <summary>
/// A base class for the singleton design pattern.
/// </summary>
/// <typeparam name="T">Class type of the singleton</typeparam>
public abstract class SingletonBase<T> where T : class
{
#region Members
/// <summary>
/// Static instance. Needs to use lambda expression
@iamandycohen
iamandycohen / gist:5643966
Created May 24, 2013 14:36
Fix jQuery Validate
// fix jquery validate reset form (this is a snippet that i created a while ago
$.extend($.validator.prototype,
{
optional: function (element) {
return !$.validator.methods.required.call(this, $.trim(element.value), element); // && "dependency-mismatch";
},
resetForm: function () {
if ($.fn.resetForm) {
$(this.currentForm).resetForm();
// fix
@iamandycohen
iamandycohen / gist:5699393
Created June 3, 2013 16:30
FileUploadFor
using MvcInputExtensions = System.Web.Mvc.Html.InputExtensions;
public static class InputExtensions
{
public static MvcHtmlString FileUploadFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
{
return MvcInputExtensions.TextBoxFor<TModel, TProperty>(html, expression, htmlAttributes: new { type = "file" });
}
}