Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Deepu Madhusoodanan deepumi

🎯
Focusing
View GitHub Profile
@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;
@deepumi
deepumi / Extensions.cs
Last active Aug 16, 2019
HttpStatus code enum -> int & string
View Extensions.cs
using static System.Net.HttpStatusCode;
internal static class Extensions
{
internal static string AsString(this HttpStatusCode status) => status switch
{
Continue => nameof(Continue),
SwitchingProtocols => nameof(SwitchingProtocols),
Processing => nameof(Processing),
EarlyHints => nameof(EarlyHints),
@deepumi
deepumi / Logger.cs
Created Aug 13, 2019
Logging class for CLI tools!
View Logger.cs
internal sealed class Logger : ILogger
{
private readonly FileStream _writer = new FileStream(Config.Configuration["LogFile"], FileMode.Create, FileAccess.Write,
FileShare.None, 1024, FileOptions.SequentialScan);
private readonly UTF8Encoding _utf = new UTF8Encoding(true);
private readonly byte[] _line;
internal Logger() => _line = _utf.GetBytes(Environment.NewLine);
@deepumi
deepumi / Sample.ps1
Created Aug 5, 2019
Powershell sample
View Sample.ps1
([system.reflection.assembly]::loadfile("")).FullName
@deepumi
deepumi / PropertySetter.cs
Created Jun 18, 2019
Poco property setter using Expressions tress.
View PropertySetter.cs
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
internal sealed class PropertySetter<T>
{
private const BindingFlags Flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase;
private readonly Type _type = typeof(T);