Skip to content

Instantly share code, notes, and snippets.

View hasmukhlalpatel's full-sized avatar

Hasmukh Patel hasmukhlalpatel

View GitHub Profile
@hasmukhlalpatel
hasmukhlalpatel / CustomLinqProvider.cs
Last active March 19, 2025 19:06
Custom IQueryable and Custom IQueryProvider
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Titan.ODataClient
{
public class CustomLinqProvider<T> : IQueryable<T>, IQueryProvider
@hasmukhlalpatel
hasmukhlalpatel / CustomValidation.cs
Created July 8, 2020 09:23
Dotnet c# Custom Validation
public class ValidateMe : IValidatableObject
{
[Required]
public bool Enable { get; set; }
[Range(1, 5)]
public int Prop1 { get; set; }
[Range(1, 5)]
public int Prop2 { get; set; }
@hasmukhlalpatel
hasmukhlalpatel / HttpContext
Last active July 10, 2020 22:20
Blazor HttpContext null
_contextAccessor?.HttpContext is null
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1#iisiis-express-support
--
If the HttpContext null-reference exception happens after deployment to Azure App Services or IIS, you might have forgotten to enable WebSockets.
Here is the deal:
If the app runs on Windows with IIS: WebSockets must be enabled.
@hasmukhlalpatel
hasmukhlalpatel / default.aspx
Last active July 29, 2020 16:00
HTTP request inspecter, Read all headers from request- host on iis -https://webhook.site/ RequestBin. ngrok— Postman or Insomnia
<%@ Page Language="C#" AutoEventWireup="true" ClassName="Default" %>
<%
var url = string.Format("http{0}://{1}{2}", (Request.IsSecureConnection) ? "s" : "", Request.Url.Host, Page.ResolveUrl("relativeUrl"));
var headers = String.Empty;
foreach (var key in Request.Headers.AllKeys)
headers += key + "=" + Request.Headers[key] + "<BR/>";
var requestData = "";
requestData+=(string.Format("scheme={0}",Request.Url.Scheme)+ "<BR/>");
requestData+=(string.Format("Host={0}",Request.Url.Host)+ "<BR/>");
@hasmukhlalpatel
hasmukhlalpatel / JwtUtils.cs
Last active April 11, 2023 15:08
Generate and validate JWT tokens In ASP.NET Core
public interface IJwtUtils
{
public string GenerateToken(ClaimsIdentity claimsIdentity, AuthRequestContext requestContext,
out long expiresIn,
DateTime? issuedAt = null, DateTime? notBefore = null, DateTime? expires = null, double defaultExpiryMinutes = 30);
public bool ValidateToken(string token, AuthRequestContext requestContext);
}
public class JwtUtils : IJwtUtils
{
@hasmukhlalpatel
hasmukhlalpatel / ValidateToken.cs
Last active April 3, 2023 20:56
Validate JWT Auth token
//Get JWKS url from https://{authserver}/.well-known/openid-configuration
//e.g. : https://demo.identityserver.io/.well-known/openid-configuration
/*
//get public keys (e "Exponent" & n "Modulus") values "https://{authserver}/.well-known/openid-configuration/jwks"
//e.g. {"keys":[{"kty":..., "use":..., "kid":... "e":"AQAB", "n":"w7Zdfmece8iaB0kiTY...", "alg":"RS256" } ]}
//e.g. "https://demo.identityserver.io/.well-known/openid-configuration/jwks"
*/
public static void ValidateToken(string token){
string tokenStr = "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUlMyNTYifQ.ewogImlzcyI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ4Mjg5NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiAibi0wUzZfV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDEzMTEyODA5NzAsCiAiY19oYXNoIjogIkxEa3RLZG9RYWszUGswY25YeENsdEEiCn0.XW6uhdrkBgcGx6zVIrCiROpWURs-4goO1sKA4m9jhJIImiGg5muPUcNegx6sSv43c5DSn37sxCRrDZZm4ZPBKKgtYASMcE20SDgvYJdJS0cyuFw7Ijp_7WnIjcrl6B5cmoM6ylCvsLMwkoQAxVublMwH10oAxjzD6NEFsu9nipkszWhsPePf_rM4eMpkmCbTzume-fzZIi5V
@hasmukhlalpatel
hasmukhlalpatel / BsComponentBase.cs
Created June 4, 2020 22:40
Blazor bootstrap Model dialog
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
namespace MyApp.Components
{
public abstract class BsComponentBase: ComponentBase
{
protected const string StylesBtnPrimary = "btn-primary";
protected const string StylesBtnSecondary = "btn-secondary";
protected const string StylesBtnSuccess = "btn-success";
@hasmukhlalpatel
hasmukhlalpatel / BsComponentBase.cs
Last active June 4, 2020 22:41
Blazor Bootstrap dropdown
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
namespace MyApp.Components
{
public abstract class BsComponentBase: ComponentBase
{
protected const string StylesBtnPrimary = "btn-primary";
protected const string StylesBtnSecondary = "btn-secondary";
protected const string StylesBtnSuccess = "btn-success";
@hasmukhlalpatel
hasmukhlalpatel / Sample.cs
Last active June 2, 2020 14:34
Dotnet core System.Text.Json JsonSerializer options
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new StringToIntJsonConverter(), new StringToDecimalJsonConverter(), new JsonStringEnumConverter() },
IgnoreNullValues = true
};
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Parse<Person>(json, options);
@hasmukhlalpatel
hasmukhlalpatel / ConventionRouter.cs
Created May 24, 2020 21:33
Blazor custom router
using HpBlazor.Components.Shared;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
namespace BlazorApp1.Core
{