C# Archives -

Category Archives: C#

Inheritance in C#

Posted On February 15, 2023 by Sourav Patil Posted in Tagged in ,

In C#, inheritance is a way to create a new class that is a modified version of an existing class. The new class is called the derived class, and the existing class is the base class. The derived class inherits the base class members, which means it has access to all of the base class’s fields and methods. This is useful because it allows you to reuse code and avoid writing the same code over again in the derived class. To create a derived class in C#, you use the: operator, followed by the name of the base class. For example: The derived class can then use the base class members as if they were it’s own. It can also override the base class’s methods or hide them with the new keyword. In the example above, the derived class is overriding the Some Method method from the base class, which means that it is providing a new implementation for that method. The new keyword is used to hide the Some Other Method method from the base class, which means that the derived class provides its own version of the method that is separate from the one in the base class. In addition to inheriting from a single base class, a class in C# can also implement one or more interfaces. An interface is a set of related methods that a class can implement. A class that implements an interface must provide an implementation for each of the methods in the interface. In the example above, the derived class is both derived from the Base Class and implements the Isom Interface. This means that it has access to all of the members of the Base Class as well as the method defined in the Isom Interface. In C#, inheritance is a powerful tool that allows you to create new classes based on existing ones. It helps you reuse code and avoid having to write the same code over again, saving time and making your code easier to maintain.

Share Story :

Secure your Environment Variable inside Azure Function using Key Vault

In the previous blog, we learn about how to use the Environment variable in Azure Function. Environment variables are most important and confidential as they might contain the system credentials or configure that you don’t want any to access directly. The most unsecure way to store them is directly inside the Configuration of Azure function so in this blog we will see how to store the Environment variable inside the Key Vault and use the Key Vault reference in Configuration. You can create an Azure function and use Environment Variable to do so do refer to my previous blog: Step 1: Create a Key Vault Login to Azure Portal and click on + Create a resource called “Key Vault”. Click on Create You can create a new resource group or select the existing group based on your preference. (You can create all resources related to a single project in one resource group so that it will easier to manage resources project wise) You need to set up the Access policy during the creation or after creating the Key Vault. We will set up an access policy for Azure Function later in the blog. Step 2: Set an Access policy for Azure Function. Open the Azure Function in which you want to use the reference of Azure Key Vault. Navigate to Identity Tab and toggle System assigned status to “ON”. Copy the Object (Principle) ID as we are going to use it during adding access policy. Open Azure Key Vault and Navigate to the Access Policies section. Click on + Add Access Policy We will only Add Get Permission in the Secret permissions section. You need to only add the permission that you want to allow your Azure Function can perform. Select the Principle User. Copy the Copied “Object ID” into search it will be easier to find it. Click on Add. Once you will add a resource in the access policy, it will allow your resource (Azure Function) to perform a Get operation on all the secrets from Key Vault. Do not forget to Save after you add the policy. Step 3: Add Secret and Configure it inside Azure Function Navigate to the Secrets section and Click on + Generate/Import Enter the Name and Value(Credentials/Configuration) Open the current version and Copy the secret URL as it will be required while configuring the Reference. Navigate to the Configuration section of Azure Function. Change the Configuration and add the KeyVault Reference as below: @Microsoft.KeyVault(SecretUri=<Copied SecretURL>) Do not forget to Save after changing the configuration Step 4: Testing using Postman We will require the API testing tool, here I am using Postman and the following is the link to download “Postman”. https://www.postman.com/downloads/ Copy the Function URL and send a post request. As result, you will notice the username is coming from KeyVault as we have to change the configuration. Conclusion: This is how you can secure your Environment Variable using Azure KeyVault. You can set up multiple KeyVault for different deployment and access policies depending on the requirement. When you are storing the Credentials and Configuration in KeyVault, you just need to set up the Access policy. All configuration is managed by the Azure Admin and if origination has a policy that they don’t want to share the credentials they can follow this process and only share the KeyVault URL with the developer so that they only need to configure it.

Share Story :

How to call logic app in Azure function app

In this blog, we will see how we can trigger the logic app using the azure function app. Let’s start with creating a logic app (HTTP triggered) that sends an email when the logic app URL is hit in the function app. Step 1: Create a logic app resource and start by adding an HTTP GET request trigger. Url will auto-generate after saving the logic app. Step 2: Add send email block after that to receive an email Step 3: now create a function app in visual studio by creating a new project. Step 4: Add project name and now select type of function app, select the create option Step 5: Add the below code to the auto-generated code template in the visual studio Step 6: Run the function locally. then you will get URL in the command prompt copy that and paste it into your browser. Step 7: After entering the URL you should receive an email as mentioned in the logic app. In this way, the logic app triggered successfully using the function app Hope this blog helps you. Thank you!!

Share Story :

Use of Environment Variable inside Azure Function in C#

In this blog, we will learn how to configure and use Environment Variable in Azure Function. Concept of using Environment variable during your development which needs to deploy on multiple servers and connect to multiple systems. Whenever you use Environment variables to store the global constant or system credentials it reduces the time which was required to modify the code base for multiple deployments. Let’s get started with Creating the Azure Function and using the Environment variable. Step 1: Create an Azure Function inside the Visual Studio: I have created the HTTP Trigger Azure function inside Visual Studio and the authorization level is Anonymous Once you create an Azure Function project you will notice there are many files created with the project.  We have a “local.setting.json” file that we are going to use and it holds the environment variable while the development and testing phase of your Azure function. Step 2: Declare credentials/global variable inside “local.setting.json” Information on local.setting.json – This file represents your local application setting for your Azure Function and it stays with your local build only. While you publish your azure function to Azure Portal or commit your code to your Repo this file is never pushed to the server as they are excluded/prevent from committing or publishing. Step 3: Access variable inside the code You can access the environment variable using the Environment class(required System lib). Based on your preference you can either access Environment variable the directly inside the code or you can create a CONSTANT class as mentioned below screenshot. Here, I am passing this environment variable inside a response so that we can test it. Step 4: Testing using Postman We will require the API testing tool, here I am using Postman and the following is the link to download “Postman”. https://www.postman.com/downloads/ To test the application, click on the Start button on top of the Navbar as mentioned below in the screenshot [Button will have Project Name]. It will take a few minutes to Load the Azure Emulator Following is the screen you will be able to see and copy the URL highlighted in the red below and paste that URL into Postman. Result: Now, you need to configure the Environment while deploying the Azure function on Azure Portal, and below are steps for the same: Step 1: Deploy an Azure Function on Azure Portal. You can directly create it on Portal or you can create it from Visual Studio. Here I am going to create and deploy it from Visual Studio: Right Click on the project and select Publish: Select Azure and Click on Next: Click on the + icon to create a new function, You can use the existing resource group or create a new resource group as per your preference: After deployment is completed you can see the deployed Azure function on Azure Portal: Now, if you try to use the Azure Function URL and post the request then you will notice that all the environmental credentials part is missing as we have not configured the environment variable on the Azure portal inside Azure Function App: Step 2: Configure the App Environment variable on Azure Portal for online deployment. Navigate to the Configuration section of your deployed Azure Function Add the Application setting in your Configuration and the Name should same as you have declared in your local.setting.json file. Make sure your click on Save to update the configuration on Portal. After configuration if you test your deployed Azure function using postman you will get your expected result: This is how you can configure and use the Environment variable inside your Azure Function. It will make your life easier while deploying Azure on Development, UAT, or Production Environment. You can store the Source and Destination system credentials, SQL Access credentials, or Custom Global Variable based on your business requirement of integration.

Share Story :

Serializing and Deserializing Json objects with key value pairs in C#

Posted On March 30, 2022 by Shruti Gupta Posted in Tagged in ,

If we have a json data whose objects have one or more key value pairs also known as properties and we need to separate them into individual objects then Serializing and Deserializing method can be used in C#. Sample Json data :      {                                 “FirstName”:”Aditya”,                                 “MiddleName”:”Ashok”,                                 “LastName”:”Somwanshi”,                                 “Phone”:[“9004802526″,”34304235”],                                 “Address”:{“Primary”:”Panvel”, “Secondary”:”Cloudfronts”}                             } Desired Output: {   “FullName”: “Aditya Ashok Somwanshi”,   “Primary Phone”: “9004802526”,   “Secondary Phone”: “34304235”,   “Primary Address”: “Panvel”,   “Secondary Address”: “Cloudfronts” } First open Visual Studio and create new project in Console application template. Give your Project a name and click next, you will be directed to a new page where you can start with your code. Now we need to install some packages into our project to use it in our code. So at top in the tools section select NuGet Package Manager and in that select Manage Nuget Packages for Solution Now search for Newtonsoft.Json and install the package The purpose for installing this package is to access serialize and deserialize functions in our code. To add a class to main program you need to right click on your project and then select and choose add new item Select class and name the class and add. Now to write the code we need to use Convert JSON to C# Classes Online – Json2CSharp Toolkit for formatting the json to C# and knowing the classes to be used. using System.Collections.Generic; using Newtonsoft.Json; namespace Human {     public class Address     {         public string Primary { get; set; }         public string Secondary { get; set; }     }     public class Person     {         public string FirstName { get; set; }         public string MiddleName { get; set; }         public string LastName { get; set; }         public List<string> Phone { get; set; }         public Address Address { get; set; }     }     public class Root     {         public string FullName { get; set; }         [JsonProperty(“Primary Phone”)]         public string PrimaryPhone { get; set; }         [JsonProperty(“Secondary Phone”)]         public string SecondaryPhone { get; set; }         [JsonProperty(“Primary Address”)]         public string PrimaryAddress { get; set; }         [JsonProperty(“Secondary Address”)]         public string SecondaryAddress { get; set; }     } } Now the main program using Newtonsoft.Json; using System; namespace Human {     public class Program     {         public static void Main(string[] args)         {             string json = @”{                                 “”FirstName””:””Aditya””,                                 “”MiddleName””:””Ashok””,                                 “”LastName””:””Somwanshi””,                                 “”Phone””:[“”9004802526″”,””34304235″”],                                 “”Address””:{“”Primary””:””Panvel””, “”Secondary””:””Cloudfronts””}                             }”;//Defining Json             Person human = JsonConvert.DeserializeObject<Person>(json);//Deserializing Json             Root rt = new Root();             rt.FullName = human.FirstName + ” ” + human.MiddleName + ” ” + human.LastName;  //Concatenating Names             rt.PrimaryPhone = human.Phone[0];             rt.SecondaryPhone = human.Phone[1];             rt.PrimaryAddress = human.Address.Primary;             rt.SecondaryAddress = human.Address.Secondary;             string output = JsonConvert.SerializeObject(rt, Formatting.Indented);//Serializing Json             Console.WriteLine(output);         }     } } OUTPUT:

Share Story :

SEARCH BLOGS:

[gravityform id="36" ajax="true"]

FOLLOW CLOUDFRONTS BLOG :