Skip to content

Instantly share code, notes, and snippets.

View cajuncoding's full-sized avatar
💭
Released RepoDb.SqlServer.PagingOperations for Cursor Paging w/ SQL Server

Brandon cajuncoding

💭
Released RepoDb.SqlServer.PagingOperations for Cursor Paging w/ SQL Server
View GitHub Profile
@cajuncoding
cajuncoding / AssemblyAttributeSearch.cs
Last active April 1, 2020 16:41
Need to search for Attributed classes in your assembly and other related assemblies the local bin folder? This small class provides low level plumbing for things like Dynamic Factory methods (using Attributes), dynamic Plugins, etc.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace System.Reflection.AttributeSearch
{
/// <summary>
/// BBernard
@cajuncoding
cajuncoding / SimpleLazyCacheHelper.cs
Last active August 4, 2020 20:48
Super simple self-populating generic typed static caching implementation using Lazy<> & ConcurrentDictionary<>... For a full blown production ready implementation check out the LazyCacheHelpers project.
using System;
using System.Collections.Concurrent;
public class SimpleLazyCacheHelper<T> where T: class
{
private readonly ConcurrentDictionary<string, Lazy<T>> _cache = new ConcurrentDictionary<string, Lazy<T>>();
[Obsolete("This method adds existing values to the cache, but it's best to use GetOrAddToCache() to facilitate a self-populating cache (especially in a Web environment)")]
public void AddToCache(string key, T value)
{
@cajuncoding
cajuncoding / CopySecretsToAnotherKeyVault.ps
Last active August 12, 2022 02:30
Simple PowerShell Scripts for: Copying KeyVault Secrets between two Key Vaults & Deleting All KeyVault Secrets
#**************************************************************************************************
#COPY All Secrets from Source KeyVault to Destination (with ContentType)
#Originally Inspired by the StackOverflow post here: https://stackoverflow.com/a/55618194/7293142
# To Use: https://docs.microsoft.com/en-us/powershell/azure/install-az-ps
# 1) Ensure you have permission to execute in PowerShell:
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# 2) Install Azure "Az" Modeule:
# Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
#**************************************************************************************************
Param(
@cajuncoding
cajuncoding / TokenIdGenerator.cs
Last active June 8, 2023 06:15
A simple, relatively efficient, class for generating very unique Ids of arbitrary length.
using System;
using System.Linq;
using System.Text;
namespace CajunCoding.UniqueIds
{
/// <summary>
/// BBernard / CajunCoding
/// A simple, relatively efficient, class for generating very unique Ids of arbitrary length.
/// They are not guaranteed to universally unique but the risk of collisions is of no practical implication for many uses
@cajuncoding
cajuncoding / RetryWithExponentialBackoffAsync.cs
Last active June 10, 2024 21:52
Simple, but effective, asynchronous Retry mechanism with Exponential Backoff for C#
namespace CajunCoding
{
/// <summary>
/// Simple but effective Retry mechanism for C# with Exponential backoff and support for validating each result to determine if it should continue trying or accept the result.
/// https://en.wikipedia.org/wiki/Exponential_backoff
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="maxRetries">The Max number of attempts that will be made.</param>
/// <param name="action">The Func&lt;T&gt; process/action that will be attempted and will return generic type &lt;T&gt; when successful.</param>
/// <param name="validationAction">A dynamic validation rule that can determine if a given result of generic type &lt;T&gt; is acceptable or if the action should be re-attempted.</param>
@cajuncoding
cajuncoding / AsyncReaderWriterLock.cs
Last active September 6, 2023 16:49
This is an async compatible reader / writer lock based on semaphores
/// <summary>
/// Adapted from original lightweight async reader/writer implementation on Stack Overflow:
/// https://stackoverflow.com/a/64757462/7293142
/// The answered question was then improved and posted via comment here:
/// https://github.com/copenhagenatomics/CA_DataUploader/pull/90/files#diff-24a9664c904fe9276878f37dc1438aae578a76b7ef34eabbebf6ac66eaad83e6
///
/// Released under the same Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licensing as the original StackOverflow source:
/// https://creativecommons.org/licenses/by-sa/4.0/
///
/// This (@CajunCoding) version adds support for simplified using(){} notation via IDisposable so that Try/Finally blocks are not needed.
@cajuncoding
cajuncoding / PKCEGenerator.cs
Created May 22, 2023 04:52
Simple PKCE Generator for Code Verifier & Code Challenge tokens in C# (to be used in the Auth Code Authentication Flow with Azure AD, etc.)
using System;
using System.Security.Cryptography;
using System.Text;
namespace CajunCoding.PKCE
{
/// <summary>
/// BBernard / CajunCoding
/// A simple class to generate Proof Key for Code Exchange (PKCE) codes to be used in the Authorization code authentication flow.
///
@cajuncoding
cajuncoding / HMACGenerator.cs
Last active April 23, 2024 03:52
Simple Class to create a well formed HMAC with SHA256 encryption and HEX Encoding
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace CajunCoding.HMAC
{
/// <summary>
/// BBernard / CajunCoding
/// A simple class to generate HMAC codes (hash-based message authentication code) to be used for signing and validating
@cajuncoding
cajuncoding / AzFuncGraphQLRoleAuthorizationFramework.cs
Last active October 30, 2023 19:50
Simplified Role based Authorization for HotChocolate GraphQL v13 in Azure Functions
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using HotChocolate;
using HotChocolate.Resolvers;
using HotChocolate.Data.Projections.Context;
@cajuncoding
cajuncoding / AzFuncGraphQLConsoleLoggingEventListener.cs
Last active February 2, 2024 05:06
Azure Functions Console/Stream Logger for Hot Chocolate GraphQL v13
using HotChocolate.Execution.Instrumentation;
using HotChocolate.Execution;
using System;
using System.Diagnostics;
using System.Linq;
using Aeg.Common.CSharp.CustomExtensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using GraphQL.HotChocolate.AzureFunctions.Authorization;
using System.Security.Claims;