Skip to content

Instantly share code, notes, and snippets.

View cajuncoding's full-sized avatar

Brandon cajuncoding

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 / 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 / LocalSettingsEnvironmentReader.cs
Last active September 14, 2023 20:35
Load the local.settings.json into Environment Variables (Very Helpful for Testing AzureFunctions)
using System;
using System.IO;
using Newtonsoft.Json.Linq;
namespace CajunCoding.Configuration
{
public static class LocalSettingsEnvironmentReader
{
/// <summary>
/// BBernard
@cajuncoding
cajuncoding / AsyncLazy.cs
Last active September 21, 2023 01:36
Ultra lightweight AsyncLazy<T> Wrapper for Lazy<T>
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace CajunCoding
{
/// <summary>
/// Greatly Simplified lightweight wrapper class to support improved code readability and management of
/// Async methods used in combination with Lazy&lt;T&gt; wrapper for thread safe lazy loading from a Func value factory!
/// Based on Original blogs here:
/// https://blogs.msdn.microsoft.com/pfxteam/2011/01/15/asynclazyt/
@cajuncoding
cajuncoding / IEnumerableChunkCustomExtensions.cs
Created September 22, 2023 05:51
Transform any IEnumerable set into an IAsyncEnumerable with Async Transformation in Chunks (e.g. Async stream REST/GraphQL API results in Chunks)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Channels;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
namespace CajunCoding.Async
{
@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;
@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 / RegionInfoFinder.cs
Last active May 21, 2024 20:17
Simple helper to find and convert RegionInfo details using two or three letter Country Codes
using System;
using System.Globalization;
using System.Linq;
namespace CajunCoding
{
/// <summary>
/// Simple helper to find and convert RegionInfo details using two or three letter Country Codes.
/// NOTE: Inspired but fully rewritten from the Gist: https://gist.github.com/BenjaminAdams/277928c7ad291ea7aea8
/// </summary>