ASP.NET Core Data Protection using IDataProtectionProvider With Example
ASP.NET Core Data Protection using IDataProtectionProvider With Example
Find this Demo on Github
This Process is easier for developers to use solid Cryptographic algorithm to make safe their data in the case you need to encrypt/decrypt your data frequently.
With this,
- If you need to encrypt data, simply pass the data into the protect method.
- If you need to access the data again, pass the encrypted data into the Unprotect method, and it will be converted back into plaintext.
By default, it uses 256-bit AES encryption to protect data, which is one of the best choices for an algorithm and It hides all the background task from the developer. Because When you encrypt data, key management becomes a concern.
Step 1
Create a new console application in .Net core.
Step 2
Run the below commands in the package manager console.
Install-Package Microsoft.Extensions.DependencyInjection
Install-Package Microsoft.AspNetCore.DataProtection
Use following Namespace on your class.
using System;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
If we see on Microsoft.AspNetCore.DataProtection namespace we can see one interface; IDataProtectionProvider and it contains one method CreateProtector.
Also, we can see one more interface, IDataProtector, which inherits the IDataProtectionProvider interface. Which includes two different method definitions.
namespace Microsoft.AspNetCore.DataProtection
{
public interface IDataProtector : IDataProtectionProvider
{
byte[] Protect(byte[] plaintext);
byte[] Unprotect(byte[] protectedData);
}
}
In the above code snippet, you can see that there are two methods
- Protect - Cryptographically protects a piece of plaintext data.
- Unprotect - Cryptographically unprotects a piece of protected data
Step 3
your main class file look like this
using System;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
namespace DataProtection
{
class Program
{
static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection();
var services = serviceCollection.BuildServiceProvider();
var instance = ActivatorUtilities.CreateInstance<TestClass>(services);
instance.RunSample();
}
public class TestClass
{
IDataProtector _protector;
// the 'ObjProvider' parameter is provided by DI
public TestClass(IDataProtectionProvider ObjProvider)
{
_protector = ObjProvider.CreateProtector("Prakash");
}
public void RunSample()
{
//Read Inputs
Console.Write("Enter input: ");
string input = Console.ReadLine();
//Protect
string Stringprotected = _protector.Protect(input);
Console.WriteLine($"Protected String: {Stringprotected}");
//Unprotect
string StringUnProtected = _protector.Unprotect(Stringprotected);
Console.WriteLine($"Unprotected String: {StringUnProtected}");
Console.ReadLine();
}
}
}
}
In the main method, we are adding data protection services with the help of Dependency Injection and also creating an instance for TestClass using a service provider. Using this instance, we are calling TestClass methods.
Step 4 (output)
Let's Enter one plain text. Then we can see out Protected results as well as Unprotected Plain text as output.
Summary: Finally we can encrypt and decrypt out input.
Also Read:
- Get file size in bytes in C# using the FileInfo.Length property
- File / Images Upload REST API using ASP.NET Core 3.0
- Export C# list into CSV (Comma Separated Values) in ASP.NET CORE 3.0 Web Application
- Cookie Authentication and authorization with password encryption and decryption using ASP.NET core