<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dilan Perera on Medium]]></title>
        <description><![CDATA[Stories by Dilan Perera on Medium]]></description>
        <link>https://medium.com/@dilanperera92?source=rss-ca39fbc9d8f1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HKXOMLH6gbutal5APBd8Ag.jpeg</url>
            <title>Stories by Dilan Perera on Medium</title>
            <link>https://medium.com/@dilanperera92?source=rss-ca39fbc9d8f1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:23:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dilanperera92/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AutoMapper in ASP.NET Core]]></title>
            <link>https://dilanperera92.medium.com/automapper-in-asp-net-core-9fdaf4be0096?source=rss-ca39fbc9d8f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fdaf4be0096</guid>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[webapi]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[automapper]]></category>
            <dc:creator><![CDATA[Dilan Perera]]></dc:creator>
            <pubDate>Fri, 24 Sep 2021 00:33:39 GMT</pubDate>
            <atom:updated>2021-09-24T00:33:39.220Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*-AkoCb9NYYXjxD_i6AYcaw.png" /></figure><p>AutoMapper is an object to object mapper. It allows to map an object of Type X to Type Y. AutoMapper is a nuget package we can install using Nuget Package Manager in VS.</p><p>Have a look -<a href="https://www.nuget.org/packages/AutoMapper/">https://www.nuget.org/packages/AutoMapper/</a></p><p>In this article I’ll describe how to use the AutoMapper in a .NET Core Web API.</p><p>I’m building a WebAPI to return Student data from SQL Server.</p><p><strong>Step 1: </strong>Create a .NET Core class library (mine is AspNetCoreWebApi.Models) and create the Student model inside it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CQl1dzjWSNxzXLMpilKkFg.png" /></figure><p><strong>Step 2:</strong> Create another class library (mine is AspNetCoreWebApi.Services). Inside that create a folder called Models and create the StudentDto class inside it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*PstJq0dZkBpLIGZ5tkXr7w.png" /></figure><p>Now what is the different between <strong>Student </strong>and <strong>StudentDto</strong>?</p><p>Here I use the the concept of <strong>Data Transfer Objects(Dto). </strong>Dto model will be used to expose to the client rather than the actual model. That can be due to various reasons.</p><p>Ex :Hide particular properties that clients are not supposed to view, Omit some properties in order to reduce payload size etc..</p><p>refer <a href="https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5">https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5</a> for more info.</p><p>Here my StudentDto model is the one that I’ll be using to expose to the client. If you see the properties of both of the models above StudentDto has the FullName property instead of FirstName and Last Name.</p><p><strong>Step 3:</strong> Install AutoMapper to the AspNetCoreWebApi.Services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4-zztxz_TSQ9v1YTIPvRaw.png" /></figure><p><strong>Step 4: </strong>Create another folder called Profiles in AspNetCoreWebApi.Services. Inside that create a class called StudentProfile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zSDQaxa7Fg-naUbv-_5sJA.png" /></figure><p>This class implements the Profile class from AutoMapper. Here you can see, I have mapped the source model of Student to destination model of StudentDto. Further I have mapped the StudentDto’s FullName from FirstName and LastName from the Student model as a combination.</p><p>Step 5: Install AutoMapper.Extensions.Microsoft.DependencyInjection in WebAPI project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OHTZP2cEyX13zVDrSpfLPQ.png" /></figure><p>Step 6: In the Startup file of the WebAPI, add the following middleware to configure the AutoMapper to use as a Dependency Injection in the StudentController. (See the constructor of StudentController in Step 7.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XKGWKSsL1ryxgZ0u3S5j-g.png" /></figure><p><strong>Step 7:</strong> Create the StudentController to write the action methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xferUXJs7nihhsTL2oQPsw.png" /></figure><p>So like wise we can use the AutoMapper to map objects.</p><p>Now if you run the WebAPI you can see the swagger UI exposes the StudentDto documentation to the client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZhWj4PaYqttIjxm7GGUNrw.png" /></figure><p>That’s a wrap… Thank You.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fdaf4be0096" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection(DI) in .NET]]></title>
            <link>https://dilanperera92.medium.com/dependency-injection-di-in-net-331024621db8?source=rss-ca39fbc9d8f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/331024621db8</guid>
            <category><![CDATA[dot-net-core]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[dot-net-framework]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Dilan Perera]]></dc:creator>
            <pubDate>Sun, 05 Sep 2021 06:09:47 GMT</pubDate>
            <atom:updated>2021-09-05T06:54:29.240Z</atom:updated>
            <content:encoded><![CDATA[<h3>Dependency Injection(DI) in .NET</h3><h3>What is DI?</h3><p>DI is a popular design pattern which is a great way to reduce tightly coupling between software components.</p><p>What is tightly coupled?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yMN6a3-h9YdbgJ-0TmPnZg.png" /><figcaption>Direct Dependency</figcaption></figure><p>Above is a three tier architecture where all layers are tightly coupled. Which is Client uses Business Layer and Business Layer uses Data Access Layer so on. Hence, if the Business Layer is broken then the whole system collapse. Or if a modification has been done for the Business Layer, then that will affect on both Client and the Data Access Layer.</p><p>It will be very problematic to change the development of this kind of scenario in compile time and run time, when a team is working on a solution like above.</p><p>We use Dependency Injection to overcome this issue.</p><h3>How DI solves the tightly coupled issue?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bYKAKSgJdtB3jXRQ0kuWow.png" /><figcaption>Inverted Dependency</figcaption></figure><p>Now the Class has the reference to an Interface and depends on it. If the Class B and Class C are not there in the compile time, it doesn’t affect on Class A since it depends on the Interface B.</p><p>We can inject the implementation of Class B to Interface B at the runtime and then Class A can use the functionality. This is called as Inverse of Control (IOC).</p><p>We use a IOC container (DI container)to arrange our interfaces and classes.</p><p>Ex: Unity, Autofac, Ninject etc..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*xlNJnGDKyGAAisVI08pREw.png" /><figcaption>DI pattern</figcaption></figure><p>Here the Client will be using the Service. The Injector will create an instance of the Service and inject to the Client. So that the direct dependency between Client and Service is gone, since the Client references an interface of a Service.</p><p>And the implementation of the Service doesn’t matter if we ask to inject and the current implementation of it, the Injector will Inject it to the Client.</p><p>So the Injector takes the responsibility of Client uses the Service here.</p><p>This is the working pattern of Dependency Injection.</p><h3>DI Types</h3><p>•<strong>Constructor Injection </strong>- <em>Inject the instance of the dependency class in the constructor of the dependent class.</em></p><p>•<strong>Property Injection </strong>- <em>Inject the instance of the dependency class in a property of the dependent class.</em></p><p>•<strong>Method Injection </strong>-<em> Inject the instance of the dependency class in a method/action of the dependent class.</em></p><p>So it covers about the Dependency Injection.</p><p>Thank you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=331024621db8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to secure an ASP.NET Core WebAPI using JWT]]></title>
            <link>https://dilanperera92.medium.com/how-to-secure-an-asp-net-core-webapi-using-jwt-f64eba1589eb?source=rss-ca39fbc9d8f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f64eba1589eb</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[jwt-auth]]></category>
            <category><![CDATA[jwt-token]]></category>
            <category><![CDATA[net-core-5]]></category>
            <category><![CDATA[web-api-development]]></category>
            <dc:creator><![CDATA[Dilan Perera]]></dc:creator>
            <pubDate>Wed, 01 Sep 2021 05:37:00 GMT</pubDate>
            <atom:updated>2021-09-01T05:37:00.766Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is JWT?</h3><p>JWT is Json Web Token. Which is a very popular and powerful authorization mechanism.</p><h3>How JWT works?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*rAcTgze3OvV-hrK2RqHOWA.png" /><figcaption>How JWT works across Client and Server</figcaption></figure><p>Here the benefit is sever doesn’t have to store user in a session in it’s memory. Instead of that server passes the token over to the client. And when user sends requests, the server doesn’t have to lookup for the user based on the session id since the JWT has the user info stored in. So the server doesn’t have to remember anything which is really great.</p><p><strong>Let’s secure our .NET Core WebAPI</strong></p><p><strong>Step 1:</strong></p><p>Open Visual Studio and create an ASP.NET Core Web API project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*TNKRNZjSTesBFM30p_S8DA.png" /></figure><p>It’s always a good practice to enable <strong>OpenAPI</strong> support. So that we can have the API online swagger documentation enabled.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*3U61jxMNgOzZrf-pEW_fdg.png" /></figure><p>Step 2:</p><p>Get rid of auto generated WeatherForcast controller and class and create a new controller.</p><p>First we need to enable authentication. For that go to Startup .cs and enable authentication like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*w7OvhIJ3Ge3DS_4y-QY5ww.png" /></figure><p>Step 3:</p><p>Then we need create an authentication manager interface which will include the authentication mechanism. Create a method called Authenticate which will accepts user name and the password as inputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*aVF6ab5-NUqcNRrwzlZwLQ.png" /></figure><p>Then install the Microsoft.AspNetCore.Authentication and System.IdentityModel.Tokens.Jwt nuget packages in the project.</p><p>Then create a class to implement the created interface.</p><p>For this example I’ll use data for username and password from a internal data store(From a Dictionary).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*qvOmqx7PuYFvwHH8Zay9bQ.png" /></figure><p>Then we need to implement the Authenticate method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XFnUSgT9wUoMO-Jm2Pb2TA.png" /></figure><p>Step 4:</p><p>Come back to the controller you created. And create the constructor which IJwtAuthenticationManager will be accepting as the parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*6jMNWE7272Hsy64DxKKlYg.png" /></figure><p>Create a separate model called to hold the UserName and the Password.</p><p>And use _jwtAuthenticationManager to access the Authenticate method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*78KOOstrPUfwy6oO3uT3Uw.png" /></figure><p>Step 5:</p><p>We need to define the key (in Step 3)which is needed to be encrypted and configure the middleware with JWT pipeline. For that go to Startup.cs again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gyeZFx9G-vBZHx2nfKa2-w.png" /></figure><p>Step 6:</p><p>Add the Authorization attribute to the controller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XZULPhtSudIsw_GAKEESVg.png" /></figure><p>I have defined a action in my controller like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*qQui1wrfAMt7-qVPr1m5CA.png" /></figure><h3>Now let’s test what we have done so far…</h3><p>We will use <a href="https://www.postman.com/">Postman</a> to test the API.</p><p>Let’s try to access the Get() controller action</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TR4TD3hFRXsI0lDVNE7WfQ.png" /></figure><p>You can see now we are not allowed to access the Get() action. Status is 401.(Unauthorized).</p><p>Which means we need to provide authorization to the user. Hence we need to access the Authenticate action first.</p><p>Create a new POST action and set the Content-Type header as application/json.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uqZiG-XQvMzEH5lx7Be7qg.png" /></figure><p>First we will test with an invalid credential.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*4dhvP5itlWftciqGPMViXQ.png" /></figure><p>Lets send the request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d3-mAF4-jwUrDGkzo9OPqA.png" /></figure><p>See.. Postman says its unauthorized.</p><p>Ok. Let’s use valid credential from the users Dictionary (Step 3)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qqJ43yBkYUDsCrzs6AmI4g.png" /></figure><p>Now the server has returned the JWT to us. We can use the token to access the Get() action.</p><p>Copy the token and use the token as an Authorization header value prefix with the ”Bearer ” key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VQowjU-e1GZdGC-lG0-otA.png" /></figure><p>Now you can see the expected result is returned. Which means you are authorized to access the Get() action.</p><p>Further you can go to <a href="https://jwt.io/">https://jwt.io/</a> and decode the JWT. You can see what it contains in Header and Payload.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*57LFETm8XLv-gRiwKeBtgg.png" /></figure><h3>And that’s a wrap… Hope this will help for you.</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f64eba1589eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>