Skip to content

Instantly share code, notes, and snippets.

View deepumi's full-sized avatar
🎯
Focusing

Deepu Madhusoodanan deepumi

🎯
Focusing
View GitHub Profile
@deepumi
deepumi / PasswordResetUrl.txt
Created April 20, 2022 16:23
B2C Password Reset URL
{policy} = "Azure Password Reset Policy";
{0} = "B2C directory name";
{client_id} = B2C Client Id;
https://{0}.b2clogin.com/{0}.onmicrosoft.com/oauth2/v2.0/authorize?p={policy}&client_id=&redirect_uri=https://localhost:44310/oauth/passwordresetresponse&scope=openid&response_type=id_token&prompt=login&response_mode=form_post
@deepumi
deepumi / CreditCardExpiryValidator.cs
Last active October 4, 2021 14:20
Validate Credit Card Expiry Month and Year.
using System;
public class Program
{
public static void Main()
{
var x = new CardExpiryValidator("10/2021");
Console.WriteLine(x.IsExpired);
}
}
@deepumi
deepumi / PassesLuhnTest.cs
Created September 26, 2021 15:52
The Luhn Algorithm in C#
private static bool PassesLuhnTest(string cardNumber)
{
ReadOnlySpan<char> value = cardNumber;
var sum = 0;
var isDouble = false;
for (var i = value.Length - 1; i >= 0; i--)
{
int digit;
internal static class QueryBuilder
{
internal static string Build(string uri, IDictionary<string, string> queryString)
{
var sb = new StringBuilder(uri);
var hasQuery = uri.IndexOf('?') != -1;
foreach (var item in queryString)
{
@deepumi
deepumi / BearerAuthorizeFilter.cs
Created May 24, 2020 08:06
Custom bearer authorize filter for ASP.NET Core 5.0
public sealed class BearerAuthorizeFilter : IAsyncAuthorizationFilter
{
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
if (context?.HttpContext?.Request?.Headers == null) throw new ArgumentNullException(nameof(context));
if (!context.HttpContext.Request.Headers.ContainsKey("Authorization"))
context.Result = CreateUnauthorized();
var policyEvaluator = context.HttpContext.RequestServices.GetRequiredService<IPolicyEvaluator>();
@deepumi
deepumi / BearerPolicyEvaluator.cs
Created May 24, 2020 07:04
Bearer Authentication policy for ASP.NET Core 5.0
public sealed class BearerPolicyEvaluator : IPolicyEvaluator
{
private const string Scheme = "Bearer";
public Task<AuthenticateResult> AuthenticateAsync(AuthorizationPolicy _, HttpContext context)
{
if (!context.Request.Headers.ContainsKey("Authorization"))
return Task.FromResult(AuthenticateResult.Fail("No Authorization header found!"));
string authHeader = context.Request.Headers["Authorization"];
@deepumi
deepumi / JsonContent.cs
Created April 24, 2020 07:10
Json serialized JsonContent
public sealed class JsonContent : StreamContent
{
private static MediaTypeHeaderValue JsonMediaType => new MediaTypeHeaderValue("application/json");
public JsonContent(Stream stream) : base(stream)
{
Headers.ContentType = JsonMediaType;
}
public static JsonContent Create(object value)
@deepumi
deepumi / IntegerArray.cs
Last active April 15, 2020 19:47
Convert StringArray to Integer Array
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Running;
namespace App
{
public sealed class Program
{
@deepumi
deepumi / RepositoryPattern.cs
Last active March 20, 2020 08:45
Repository pattern
//Insert new record to database
public interface ICreatableRepository<T>
{
int Create(T value);
}
//List records
public interface IListableRepository<T>
{
IList<T> List(DataParameterDictionary dataParameters);
@deepumi
deepumi / FeedPagerService.cs
Created October 10, 2019 12:13
Alternative to C# LINQ Skip() & Take() for pagination objects
internal static class FeedPagerService
{
private const int PageSize = 10;
internal static (List<FeedItem> feeds, int pageSize, int maxRecords) SkipRecords(int pageIndex, List<FeedItem> feeds)
{
if (pageIndex < 0) pageIndex = 0; //fix negative
var startPage = pageIndex <= 1 ? 0 : (pageIndex - 1) * PageSize;