Posts

Data Protection API (DPAPI) system in ASP.NET Core

Image
Over the year’s developers are struggling to secure web applications and their data. In the windows desktop application system, we have a Data Protection API (DPAPI) but not in the web applications but ASP.NET Core 2.0 release brought more goodies to developers in the realm of cryptography as Microsoft has added DPAPI to make it easier for developers to use strong cryptography to safeguard their data.  In this article, we will discuss What is the data protection system. How does the data protection system work. Why do we need the data protection system. I mplementation of the data protection system in ASP.NET Core project. Custom configuration of the data protection system.     What is data-protection system?      The data-protection system is a set of cryptography APIs used by the ASP.NET Core to encrypt/ decrypt the sensitive data. So, it is all about how to protect sensitive information that will be exposed to the attackers, ideally without exposing an...

MERGE Statement - UPSERT into SQL Server

Image
Normally, when you want to write data to a table from the application you must first do a SELECT to check if the row exists, if it does exist you execute an UPDATE and if it does not exist you execute an INSERT, which is the standard SELECT-UPDATE-INSERT pattern. Check the below query: This looks like a pretty logical flow that reflects how we think about this in real life: Does a row already exist for this key? YES : OK, update that row. NO : OK, then add it. But this is a wrong practice. Locating the row to confirm it exists, only to have to locate it again to update it, is doing  twice the work  for nothing. Even if the key is indexed (which we hope is always the case). If we put this logic into a flow chart and associate, at each step, the type of operation that would have to happen within the database, we would have this: From a developer point of view, we really enjoy the freedom, if we have, of making a single call to the database and be able to Insert/Update the data w...

Using Dependency Injection in Asp.Net Core

Image
What is Dependency Injection? The dependency injection (DI) is a software design pattern, which is a technique for achieving Inversion of control (IOC) between classes and their dependencies. ASP.NET Core is designed from scratch to support Dependency Injection. ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container. Why use Dependency Injection? Using Dependency Injection, we make a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. In other terms, the intent of Dependency Injection is to make code maintainable by allowing us to develop loosely-coupled code. Technically saying, DI reduces the hard-coded dependencies among the classes by injecting those dependencies at run time instead of design time. Overview of Dependency Injection A dependency is any object, that another object requires. Dependency Injection allows the creation of dependent objects...

Querying Cross Databases in Azure SQL

Image
Querying Cross-Databases in Azure SQL Database From past few days I am getting this question again and again that how to get data from multiple databases in Azure SQL Database. In other words, how to set up cross-database queries in Azure SQL Database. I am quite familiar with Cross-Database Queries in SQL Server as it is required in most of my projects, mostly for reporting purpose but the same process is not allowed in Azure SQL. Let’s have a look what we already have SQL Server: Here I have two databases TestDBFirst and TestDBSecond within same SQL Server instance and both of them are accessible by same SQL user. In TestDBFirst, I have Employee and Salary tables but information about their Department is in TestDBSecond. However, I have DepartmentID in Employee table for Employee-Department mapping. Now have a look at below stored procedure definition In this procedure I am using an INNER JOIN on two tables which are in different databases and execution of this pr...