Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Deepu Madhusoodanan deepumi

🎯
Focusing
View GitHub Profile
@deepumi
deepumi / PasswordResetUrl.txt
Created Apr 20, 2022
B2C Password Reset URL
View PasswordResetUrl.txt
{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 Oct 4, 2021
Validate Credit Card Expiry Month and Year.
View CreditCardExpiryValidator.cs
using System;
public class Program
{
public static void Main()
{
var x = new CardExpiryValidator("10/2021");
Console.WriteLine(x.IsExpired);
}
}
@deepumi
deepumi / PassesLuhnTest.cs
Created Sep 26, 2021
The Luhn Algorithm in C#
View PassesLuhnTest.cs
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;
View QueryBuilder.cs
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
Custom bearer authorize filter for ASP.NET Core 5.0
View BearerAuthorizeFilter.cs
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
Bearer Authentication policy for ASP.NET Core 5.0
View BearerPolicyEvaluator.cs
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 Apr 24, 2020
Json serialized JsonContent
View JsonContent.cs
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 Apr 15, 2020
Convert StringArray to Integer Array
View IntegerArray.cs
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 Mar 20, 2020
Repository pattern
View RepositoryPattern.cs
//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 Oct 10, 2019
Alternative to C# LINQ Skip() & Take() for pagination objects
View FeedPagerService.cs
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;