Tuesday, May 16, 2023

Pass Azure KeyVault Secret to Database Settings configuration

 To inject the KeyVault secret into the DatabaseSettings object

#1 You can write down the code as follow in program.cs file , configuration method 

var keyVaultEndPoint = new Uri(builder.Configuration["VaultKey"]); var secretClient = new SecretClient(keyVaultEndPoint, new DefaultAzureCredential()); KeyVaultSecret kvs = secretClient.GetSecret(builder.Configuration["SecretName"]); string connectionString = kvs.Value; builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor() .AddMicrosoftIdentityConsentHandler(); builder.Services.Configure<DatabaseSettings>(options => { options.ConnectionString = connectionString; builder.Configuration.GetSection("Database").Bind(options); }); builder.Services.AddSingleton<TodoService>(); builder.Services.AddSingleton<RecipesService>(); builder.Services.AddSingleton<SpecialDatesService>();


#2. Modify the DatabaseSettings class in your appsettings.json file:


"Database": { "ConnectionString": "", "DatabaseName": "Personal", "TodoCollectionName": "todo", "RecipesCollectionName": "recipes", "SpecialDatesCollectionName": "specialdates" }


By binding the DatabaseSettings options, you can set the ConnectionString property using the retrieved value from the KeyVault secret while keeping the rest of the configuration intact.


Now, when you inject the DatabaseSettings object into your services, the ConnectionString property will be populated with the secret value from Azure Key Vault.

Monday, May 15, 2023

default datetime in mysql

 ALTER TABLE <TABLE_NAME> 

CHANGE COLUMN <COLUMN_NAME> <COLUMN_NAME> DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;

How to create an AKS cluster in Azure?

 

To create an Azure Kubernetes Service (AKS) cluster, you can use either the Azure portal, Azure CLI, or Azure PowerShell. Here are the steps for each method:

  1. Azure Portal:
    • Sign in to the Azure portal (https://portal.azure.com).
    • Click on "Create a resource" in the left navigation pane.
    • Search for "Azure Kubernetes Service" and select it from the search results.
    • Click on "Create" to start the AKS cluster creation wizard.
    • Provide the necessary information, such as subscription, resource group, cluster name, region, and Kubernetes version.
    • Configure the desired node size, node count, and authentication method.
    • Review the settings and click on "Review + Create" to validate the configuration.
    • Finally, click on "Create" to create the AKS cluster. The deployment may take several minutes to complete.
  2. Azure CLI:
    • Open the Azure CLI (command-line interface) on your local machine or use the Azure Cloud Shell (https://shell.azure.com).
    • Run the following command to create an AKS cluster:

az aks create --resource-group <resource-group-name> --name <cluster-name> --node-count <node-count> --node-vm-size <node-vm-size> --location <region>

Replace <resource-group-name> with the name of the resource group where the cluster should be created, <cluster-name> with the desired name for the cluster, <node-count> with the number of nodes in the cluster, <node-vm-size> with the VM size for the nodes, and <region> with the desired region for the cluster.

    • Optionally, you can add more parameters to the command to configure advanced settings like networking, authentication, and monitoring.
  1. Azure PowerShell:
    • Open the Azure PowerShell module on your local machine or use the Azure Cloud Shell (https://shell.azure.com).
    • Run the following command to create an AKS cluster:

New-AzAksCluster -ResourceGroupName <resource-group-name> -Name <cluster-name> -NodeCount <node-count> -NodeVmSize <node-vm-size> -Location <region>

Replace <resource-group-name> with the name of the resource group, <cluster-name> with the desired name for the cluster, <node-count> with the number of nodes in the cluster, <node-vm-size> with the VM size for the nodes, and <region> with the desired region.

    • You can also provide additional parameters to the command to configure networking, authentication, and other advanced options.

After executing the appropriate command, the AKS cluster creation process will start, and it may take several minutes to complete. Once the cluster is created, you can access and manage it using the Azure portal, Azure CLI, Azure PowerShell, or the Kubernetes command-line tool (kubectl).

How to configure load balancer in Azure Kubernetes Service ?

 

To configure a load balancer in Azure Kubernetes Service (AKS), you can follow these steps:

  1. Create an AKS cluster: Start by creating an AKS cluster using the Azure portal, Azure CLI, or Azure PowerShell. Make sure to specify the desired configuration, such as the number of nodes, node size, and networking options.
  2. Deploy your application: Once the AKS cluster is created, deploy your application or services to the cluster. You can use Kubernetes manifests (YAML files) to define your application deployment, services, and any necessary ingress resources.
  3. Create a Kubernetes service: To expose your application to the external world and load balance the traffic, you need to create a Kubernetes service. A service defines a stable network endpoint that receives traffic and distributes it to the appropriate pods.

Here's an example of a Kubernetes service manifest that exposes your application on a specific port:

apiVersion: v1

kind: Service

metadata:

  name: my-app-service

spec:

  type: LoadBalancer

  ports:

    - port: 80

      targetPort: 8080

  selector:

    app: my-app

In this example, the service is defined as type LoadBalancer, and it exposes port 80, which gets mapped to the target port 8080 on the pods labeled with app: my-app.

  1. Apply the service manifest: Apply the service manifest using the kubectl apply command to create the service in the AKS cluster. The Kubernetes service controller will automatically provision an Azure Load Balancer and configure the necessary routing rules.

kubectl apply -f service.yaml

  1. Verify the load balancer: Once the service is created, you can check the status and details of the load balancer using the Azure portal, Azure CLI, or Azure PowerShell. Look for the provisioned Load Balancer resource associated with your AKS cluster.
  2. Access your application: After the load balancer is provisioned and configured, it will route the incoming traffic to the pods running your application. You can access your application using the public IP address or DNS name associated with the load balancer.

That's it! You have now configured a load balancer for your application in Azure Kubernetes Service. The load balancer will evenly distribute incoming traffic to the pods, ensuring high availability and scalability for your application.

Sunday, May 14, 2023

Password encryption option so even DBA can’t see the password in .NET core

 In .NET Core, you can use cryptographic functions to encrypt passwords and securely store them in a database. One common approach is to use a one-way hashing algorithm with a salt. Here's a simplified example of how you can accomplish this:

1. Add the necessary NuGet package: Install the System.Security.Cryptography package to gain access to cryptographic functions.

2. Generate a salt: A salt is a random value that adds uniqueness to each hashed password, making it harder to crack. You can generate a salt using a cryptographic random number generator. Here's an example:

byte[] salt = new byte[16];

using (var rng = RandomNumberGenerator.Create())

{

    rng.GetBytes(salt);

}

3. Hash the password: Use a secure hashing algorithm, such as bcrypt, PBKDF2, or Argon2, to hash the password along with the salt. The salt should be stored alongside the hashed password in the database. Here's an example using the bcrypt algorithm:


string password = "myPassword";

string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password, salt: salt);

4. Verify a password: When a user attempts to log in, you can verify their password by comparing the stored hashed password with the newly hashed password using the same salt. Here's an example:

string userEnteredPassword = "myPassword";

bool passwordMatches = BCrypt.Net.BCrypt.Verify(userEnteredPassword, hashedPassword);

By following these steps, even a DBA with access to the database would not be able to see the original password, as it is never stored in plain text. Only the hashed password and the salt are stored, and the verification process compares the hashed values.


Batch Processing and Retry Mechanism for CSV Files in Azure

 You can consider using two Azure services for your scenario of downloading multiple CSV files, parsing them, transforming the data, and tracking the success or failure of processing: 

#1. Storage Queue with Azure Functions:

  • Azure Blob Storage can be used to store the CSV files, and a Storage Queue can manage the processing workflow.
  • Set up an Azure Function with a queue trigger to trigger the function for processing a CSV file whenever a new message arrives in the queue.
  • Implement the parsing, transformation, and writing logic for each file within the function.
  • Track the success or failure of processing by writing the status or any error information to another storage location, such as a separate blob container or a database.
  • To enable retries, configure the Storage Queue with a visibility timeout. Messages that are not deleted after processing become visible again after a specified duration, allowing for automatic retries.

#2. Azure Batch with Spot VMs:

  • Azure Batch, a managed service, enables you to run large-scale parallel and batch computing jobs.
  • Create an Azure Batch job that defines the tasks for downloading, parsing, transforming, and writing the CSV files.
  • Utilize Azure Spot VMs, which are low-priority virtual machines available at a significantly reduced price, to handle large workloads cost-effectively.
  • Azure Batch provides a mechanism to track task execution and the overall job status. Retrieve information on the success or failure of each task and programmatically handle retries if necessary.

The choice between these approaches depends on factors such as the complexity of the processing logic, workload scale, and specific requirements of your use case.


Friday, May 12, 2023

[SC] DeleteService FAILED 1072: The specified service has been marked for deletion.

 The error message you encountered ("DeleteService FAILED 1072: The specified service has been marked for deletion") indicates that the Windows service you are trying to delete has already been marked for deletion. This usually occurs when the service is in the process of being uninstalled or has encountered an issue during the uninstallation process.


To resolve this issue, you can try the following steps:


#1 . Restart the computer: A system restart can help clear any pending operations and release the service from the "marked for deletion" state. After restarting the computer, try deleting the service again using the sc command or InstallUtil tool.


#2. Check running processes: Ensure that there are no instances of the service still running in the background. Open Task Manager and check the "Processes" or "Details" tab for any processes related to the service. If you find any, terminate them and then attempt to delete the service again.


#3. Use PowerShell: If the sc command did not work, you can try using PowerShell to forcefully remove the service. Open PowerShell as an administrator and run the following command:



Remove-Service -Name ServiceName -Force

Replace "ServiceName" with the actual name of the service you want to delete. This command will attempt to remove the service forcefully.


If none of these steps work, it is possible that there might be an issue with the service installation or the system itself. In such cases, you may need to seek further assistance or consult with a Windows system administrator for guidance.

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...