Skip to content

Instantly share code, notes, and snippets.

@duncansmart
duncansmart / ManyToMany.ascx.cs
Created July 13, 2012 19:21 — forked from davidebbo/ManyToMany.ascx.cs
Dynamic Data many to many templates for EF code first
using System;
using System.ComponentModel;
using System.Web.UI;
namespace WebApplication
{
public partial class ManyToManyField : System.Web.DynamicData.FieldTemplateUserControl
{
protected override void OnDataBinding(EventArgs e)
{
@duncansmart
duncansmart / MailgunUtil.cs
Last active October 20, 2022 06:19
C# implementation of "Securing Webhooks" sample code https://documentation.mailgun.com/user_manual.html#webhooks
using System;
using System.Text;
using System.Security.Cryptography;
class MailgunUtil
{
/// <summary>
/// Authenticates incoming requests to a Mailgun webhook (https://documentation.mailgun.com/user_manual.html#webhooks).
/// </summary>
/// <example>
@duncansmart
duncansmart / add urlacl.cmd
Created September 14, 2012 22:52
Allows IISExpress site to be externally accessed (e.g. by VM or iOS device)
:: NOTE needs to be elevated
@set /p HTTP_PORT=Enter Port number [e.g. 10082]:
netsh http add urlacl url=http://*:%HTTP_PORT%/ user=everyone
netsh http add urlacl url=http://localhost:%HTTP_PORT%/ user=everyone
netsh http add urlacl url=http://%COMPUTERNAME%:%HTTP_PORT%/ user=everyone
netsh http add urlacl url=http://%COMPUTERNAME%.local:%HTTP_PORT%/ user=everyone
@pause
@duncansmart
duncansmart / MailgunTest.cs
Created September 24, 2012 18:38
Send a mail message via Mailgun using HttpClient
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
[TestFixture]
public class MyClass
{
const string DOMAIN = "samples.mailgun.org";
const string API_KEY = "key-...";
@duncansmart
duncansmart / ConsoleExec.cs
Created September 26, 2012 13:22
Command/console exec in C#
class ConsoleExec
{
public string FileName { get; set; }
public string Arguments { get; set; }
public TextWriter StdOut { get; set; }
public TextWriter StdErr { get; set; }
public ConsoleExec()
{
@duncansmart
duncansmart / MailUtil.cs
Created October 10, 2012 22:25
GetMXRecords in C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class MailUtil
{
#region DLL Imports
@duncansmart
duncansmart / gist:3982912
Created October 30, 2012 20:45
Guid compression
static string compressGuid(Guid guid)
{
// "MspzuC2oLkKjoUEgZrbJFg=="
return Convert.ToBase64String(guid.ToByteArray())
.Substring(0, 22)
.Replace('+', '-')
.Replace('/', '_');
}
@duncansmart
duncansmart / get table schema.sql
Created November 2, 2012 16:45
TSQL - Get table columns, types, primary keys, etc
select
SchemaName = schema_name(t.schema_id)
, TableName = t.name
, ColumnName = c.name
, IsPrimaryKey = (select count(*) from
sys.indexes as i join sys.index_columns as ic on i.OBJECT_ID = ic.OBJECT_ID and i.index_id = ic.index_id and ic.column_id = c.column_id
where i.is_primary_key = 1 and i.object_id = t.object_id)
, ColumnType = TYPE_NAME(c.system_type_id)
, MaxLength = c.max_length
from sys.tables t join sys.columns c on t.object_id = c.object_id
@duncansmart
duncansmart / gist:4014051
Created November 4, 2012 22:28
Ensure SQL Service Broker is enabled
static void ensureServiceBrokerEnabled()
{
var sql = @"if (SELECT is_broker_enabled FROM sys.databases WHERE name = DB_NAME()) = 0
begin
declare @sql nvarchar(max) = 'ALTER DATABASE ' + QUOTENAME(DB_NAME()) + ' SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE'
exec(@sql)
end";
using (var db = DependencyResolver.Current.GetService<IDbConnection>())
{
db.Open();
@duncansmart
duncansmart / CreateSystemRestorePoint.js
Created December 6, 2012 08:06
Create System Restore Point (Windows)
// call with %systemroot%\System32\cscript.exe, schedule with Task Scheduler
var systemRestore = GetObject("winmgmts:\\\\.\\root\\Default:SystemRestore")
var result = systemRestore.CreateRestorePoint("Scheduled restore point", 0, 100)
WScript.Echo("result " + result)