Skip to main content

Posts

Refresh Cache using Redis and BackgroundService .net 5

Install Nuget package   Microsoft. Extensions. Caching. StackExchangeRedis Creating a cache attribute [Cached(time: 10)]  public async Task<IActionResult> GetAll() => Ok(await SalesdbContext.Employees.ToListAsync()); We create an attribute called cached that accepts the expiry time for the cached data. Once the expiry time is over the response returned will be null and we will fetch the data from the database and update the cahce. [AttributeUsage(AttributeTargets.Method)]     public class CachedAttribute : Attribute, IAsyncActionFilter     {         public int Time { get; set; }         public CachedAttribute(int time)         {             Time = time;         }         public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)       ...

Reading Dynamic Object values

https://stackoverflow.com/questions/20318261/dynamic-object-with-dollar-on-string https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object https://stackoverflow.com/questions/47507667/deserialize-json-with-variable-property-names https://www.jerriepelser.com/blog/deserialize-different-json-object-same-class/ dynamic stuff = JsonConvert.DeserializeObject<dynamic>("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");             string name = stuff.Name;             string address = stuff.Address.City;             foreach (JProperty jproperty in stuff)             {                 Console.WriteLine("jproperty.Name = {0}", jproperty.Name);             }             dynamic d = s...

Versioning an API

Strategies 1. URI Each time we change the version, the clients will have to change the URL. 2. Via Query String 3. Via Headers The client needs to have a developer who knows how to deal with headers, more sophisticated approach. Also we need to create our own custom header. 4.  VIA ACCEPT HEADERS Add  the nuget package -- Microsoft.AspNetCore.Mvc.Versioning [ApiVersion("1.0")] [ApiVersion("1.1")] [ApiVersion("2.0")] public class WeatherForecastController : ControllerBase Add this to the controller to let the user know what version it supports. To the actions Specify these.         [HttpGet("sample")]         [MapToApiVersion("1.0")]         public IActionResult Getv1(string id) => Ok("Version 1");         [HttpGet("sample")]         [MapToApiVersion("1.1")]         public IActionResult Getv2() => Ok("Version ...

Custom Authorization Attribute with dependency injection

Custom Authorization attribute with Dependency resolved using HttpContext. FOR SYNCHRONOUS    public class CustomAuthFilter : AuthorizeAttribute, IAuthorizationFilter     {         public CustomAuthFilter(params string[] args)         {             Args = args;         }         public string[] Args { get; }         public void OnAuthorization(AuthorizationFilterContext context)         {             int x = this.Args.Length;             var service = context.HttpContext.RequestServices.GetRequiredService<ISample>();             string name = service.GetName();             context.Result = new UnauthorizedResult();         }     } Contr...

Azure Functions

Azure App Service also supports the concept of web jobs. Web jobs offer a simple way to get your own background tasks, such as key processing, deployed to the same service in your hosting plan that are running the web application. So what we have with web applications and web jobs is a very programmer-friendly model that makes it super easy to create and deploy multiple websites along with background processing tasks and bundle them all up onto one server to keep your costs down, but with the flexibility to scale up as the demands of your application require. Azure Functions is actually built on top of the web jobs SDK and it's hosted on the App Service platform. So in many ways you can think of it as just another part of this same offering, but with a few additional powerful new capabilities  Azure Functions is the idea of events and code. You simply supply some code, which is just a single function usually written either in C# or JavaScript, and you tell Azure Functions what ...

OpenID connect and OAuth 2 - AUTHORIZATION WITH PKCE

1. Authorization flow with PKCE https://github.com/Anish407/OpenID-Connect---Authorization-flow-with-PKCE When a browser is involved in the communication, it is not safe to store the id and access tokens in the it. Browser is not considered to be safe. Everything you send to the browser is readable and can be extracted, manipulated, and potentially exploited. That's why authorization code flow was invented.   Using that flow, tokens aren't sent in the redirect back to the client from the authorization endpoint. Instead, a code is sent. The code can then be used by the client to do a back channel request. That's a request done at the client level the browser doesn't know about. In this request, the code is exchanged for an access token at a token endpoint. When the client does the request to the token endpoint, it has to present its client ID and secret. If the OpenID scope is among the requested scopes, the token endpoint also sends the identity token ...