Skip to content

Instantly share code, notes, and snippets.

@tekguy
tekguy / ApiLoggingMiddleware.cs
Last active March 23, 2020 21:30
Middleware for ASP.NET Core 3.1 WebAPI project to log response content size to app insights
public class ApiLoggingMiddleware
{
private readonly RequestDelegate _next;
public ApiLoggingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
@tekguy
tekguy / 00. tutorial.md
Created May 10, 2019 02:18 — forked from maxivak/00. tutorial.md
Importing/Indexing database (MySQL or SQL Server) in Solr using Data Import Handler
<#
Azure Automation Runbook to Stop Azure VM
Setup the following variables in Azure Automation Runbook:
SubscriptionName, SubscriptionID, CertificateName, VMName
Import a certificate as well
#>
workflow Stop-VM
{
$VMName = "equinox"
$subscriptionName = Get-AutomationVariable -Name "SubscriptionName"
git config --global diff.tool bc4
git config --global difftool.bc4.path "c:/Program Files/Beyond Compare 4/bcomp.exe"
git config --global merge.tool bc4
git config --global mergetool.bc4.path "c:/Program Files/Beyond Compare 4/bcomp.exe"
@tekguy
tekguy / XmlDocumentPrettyExtension.cs
Created November 10, 2014 18:43
An extension to prettyify your xml document with proper indentation
public static class XmlDocumentPrettyExtension
{
public static string Prettyify(this XmlDocument xmlDocument)
{
var stringWriter = new StringWriter(new StringBuilder());
var xmlTextWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };
xmlDocument.Save(xmlTextWriter);
return stringWriter.ToString();
}
}
@tekguy
tekguy / DbContextToSqlConnection.cs
Created October 23, 2014 15:19
Get a SqlConnection from a DbContext
private SqlConnection GetSqlConnection(DbContext dbContext)
{
var ec = dbContext.Database.Connection;
var adoConnStr = ec.ConnectionString;
return new SqlConnection(adoConnStr);
}
@tekguy
tekguy / ElmahErrors.sql
Last active August 29, 2015 14:04
Elmah SQL Log Analysis
/* Useful for analyzing a Elmah logs stored in a SQL Server */
/* Version 1 */
/* --------------------------------------------------------------*/
DECLARE @StartDateTime datetime
DECLARE @EndDateTime datetime
DECLARE @HourlyThreshold int -- If error count for an hour is greater than this number display in the threshold overflow report
SET @HourlyThreshold = 10
SET @StartDateTime = DATEADD(d, -7, CAST(GETDATE() AS date))
@tekguy
tekguy / GenerateX509Cert.sh
Last active August 29, 2015 14:03
Generate cert and private key then merge using p12
# Generate Cert and Key in seperate file
# Provide the .cert to the end user
openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 -keyout QA.key -out QA.cert
# Merge cert and private key to make a p12 file, this file will be used by .NET's X509Certificate2 class, The private key will be accessible via the "PrivateKey" property
openssl pkcs12 -export -in qa.cert -inkey qa.key -out qa.p12 -name "QA"
@tekguy
tekguy / AntiForgeryTokenValidator.asp
Created July 2, 2014 16:00 — forked from lorddev/AntiForgeryTokenValidator.asp
Anti Forgery token class for classic asp
<%
' Use with a very short session (basically the page lifecycle, GET then POST)
Class AntiForgeryValidator
Private m_securityToken
Sub SetCookie()
m_securityToken = CreateWindowsGuid()
Response.Cookies("RequestVerificationToken") = m_securityToken
@tekguy
tekguy / LoggingOffACS.cs
Last active August 29, 2015 14:02
Logging off ACS
public ActionResult LogOff()
{
// Load Identity Configuration
FederationConfiguration config = FederatedAuthentication.FederationConfiguration;
// Get wtrealm from WsFederationConfiguation Section
string wtrealm = config.WsFederationConfiguration.Realm;
string wreply;
// Construct wreply value from wtrealm (This will be the return URL to your app)