ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

ASP.NET Core Data Protection using IDataProtectionProvider With Example

.Net .Net Core about 6 years ago || 12/5/2019 || 9.9 K View

  • Hide

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

 

  • 0
  • 0
  • 1
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Prakash Pokhrel

Prakash Pokhrel

https://np.linkedin.com/in/prakash-pokhrel-42a699a2

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey