Skip to main content

Posts

Showing posts from 2020

Az-204 Docker to Container registry to AKS

 1. https://github.com/Azure-Samples/azure-voting-app-redis.git -- pull this code from github  2.  it has a docker-compose file . So run docker-compose up -d  3. Create a container registry.  4. Tag the docker image to the container registry. docker tag hello-world demoacr5416.azurecr.io/hello-world-demo:v1 hello-world: name of the image demoacr5416.azurecr.io -- name of the ACR hello-world-demo:v1 -- what we want to call it inside ACR 5. push   https://pascalnaber.wordpress.com/2020/01/21/access-keyvault-from-azure-kubernetes-service-aks-with-an-asp-net-core-application-using-a-managed-identity/ VISUAL STUDIO APP TO DOCKER 1. Publish the app to a folder (publish). FROM  mcr.microsoft.com/dotnet/core/aspnet:3.1 WORKDIR  /app EXPOSE  80 EXPOSE  443 COPY  ./publish . ENTRYPOINT  [ "dotnet" ,  "ACRTOAKS.dll" ] copy the publish file into a folder called publish and paste the docker file. Then copy everything ins...

Docker

 1.  docker run --rm -it microsoft/dotnet:2-runtime  Run linux container cmd. we get a command prompt for the container. 2. share a folder from host to container-- windows to linux docker run -it --rm -v E:\Docker\code\ACRTOAKS:/abcd microsoft/dotnet:2-runtime from E:\Docker\code\ACRTOAKS to /abcd inside the container 3. Get IP Address of the container docker inspect containername docker ps --list all running containers

AZ-204 KeyVault

Rather than storing credentials in the application, we can use Azure keyvault to store user secrets. We can also use it to store  1. secret , application that stores the secrets in files can be at risk if someone gets a peek at the source code. KV happens at runtime. 2. keys (for encryption/decryption)  3. certificates (SSL certificates) 4. Storage accounts have access keys and KV can be used to manage them. 5. Azure VM disks encryption. We can encrypt the data stored on VMS using azure disk encryption. 6. In AKS  DATA PROTECTION  An Azure app service app can use certificates stored in KV to encrypt the data (in transit) or it can be used to encrypt the data stored in a database (in rest)  Access control should follow the principle of least privilege. Add new Resource => Keyvault Create a Keyvault Using Keyvault when using azure AD NUGETS:  Azure.Identity and Azure.Security.KeyVault.Secrets _______________________________________________________________...

AZ-204 Azure Redis Cache

Why should we cache? 1. Because we dont want to hit the database everytime. 2. Caches store data in memory, so its faster to query them. 3. Caches use a key value pair to store data rather than any complex data structure , So the time complexity is less. 4. Also it improves availability , as if the backend in unavailable we can still get the data from the cache. Not all of it but the most frequently used ones. So its important to make sure that we are caching the right data. Azure Cache 1. Geo Replication: The main copy remains writable whereas the other copies are maintained as readable. So not only is the data available in a new region but also available in case the primary cache is down. In such a scenario the secondary instance becomes writable. DATA Types NUGET Microsoft.Extensions.Caching.StackExchangeRedis -- allows more data types and operations  services.AddStackExchangeRedisCache(op => {                 op.Configuration = "Conn...

BLOB STORAGE

Blob stands for Binary large object. we can store files , text , videos etc. Once we create a storage account, we can create containers ex: image where we can place the image files. The image img1 can be accessed by using the above url. The storage account name has to be uniques because in order to access the blobs the we need to a unique url.. that is http[s]:// storagename .blob.core.windows,net Virtual Directories instead of hierarchical structure Blob storage doesnt support a heirarchical structure, so we can create nested folders to group our files. but we can name the files based on whatever way we want to group the files. Ex: 2020/personal/images/img.jpg In short , there are no folders. It creates virtual directories and we can access the blobs using the url based on the path. TYPES 1. Block Blobs: we cant append data to existing ones, but we can replace the old file with a new one. Ex: image , video etc. So if we store the log file as a block blob the...

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 ...