keyboard_arrow_up

p { margin-bottom: 0.25cm; line-height: 120%; }a:link { } h1 { margin-bottom: 0.21cm; }h1.western { font-family: "Liberation Serif",serif; }h1.cjk { font-family: "Droid Sans Fallback"; font-size: 24pt; }h1.ctl { font-family: "FreeSans"; font-size: 24pt; }p { margin-bottom: 0.25cm; line-height: 120%; }a:link { } Getting Started with the Data Protection APIs To be simple, protecting data has the following steps: 1. Create a data protector from a data protection provider. 2. Call the Protect method with the data you want to protect. 3. Call the Unprotect method with the data you want to convert into plain text. Most frameworks such as ASP.NET or SignalR configures the data protection system and sum it to a service container you approach via dependency injection. The following sample explains configuring a service container for dependency injection and listing the data protection stack, receiving the data protection provider via DI, developing a protector and protecting the unprotected data using System;using Microsoft.AspNetCore.DataProtection;using Microsoft.Extensions.DependencyInjection; public class Program{public static void Main(string[] args){// add data protection servicesvar serviceCollection = new ServiceCollection();serviceCollection.AddDataProtection();var services = serviceCollection.BuildServiceProvider();// create an instance of MyClass using the service providervar instance = ActivatorUtilities.CreateInstance(services);instance.RunSample();} public class MyClass{IDataProtector _protector; // the ‘provider’ parameter is provided by DIpublic MyClass(IDataProtectionProvider provider){_protector = provider.CreateProtector(“Contoso.MyClass.v1″);} public void RunSample(){Console.Write(“Enter input: “);string input = Console.ReadLine(); // protect the payloadstring protectedPayload = _protector.Protect(input);Console.WriteLine($”Protect returned: {protectedPayload}”);// unprotect the payloadstring unprotectedPayload = _protector.Unprotect(protectedPayload);Console.WriteLine($”Unprotect returned: {unprotectedPayload}”);}}} /** SAMPLE OUTPUT** Enter input: Hello world!* Protect returned: CfDJ8ICcgQwZZhlAlTZT…OdfH66i1PnGmpCR5e441xQ* Unprotect returned: Hello world! When you design a protector you should provide one or more Purpose Strings. A purpose string gives isolation between consumers, for instance, a protector designed with a purpose string of “green” would not be able to unprotect data provided by a protector with a purpose of “purple”. Examples of IDataProtectionProvider and IDataProtector are thread-safe for many callers. It is said that once a component gets a reference to an IDataProtector via a call to CreateProtector, it will use that reference for multi calls to Protect and Unprotect. IDataProtectionProvider Now the provider interface is the root of the data protection system. It cannot be directly used to protect or unprotect data. In spite, the consumer must get a reference to an IDataProtector by calling IDataProtectionProvider.CreateProtector(purpose), where the purpose is a string that defines the intended consumer use case. IDataProtector This protector interface is returned by a call to CreateProtector, and it is this interface which consumers can utilize to do protect and unprotect operations. To protect a piece of data, pass the data to the Protect method. The basic interface explains a method which transforms byte[] -> byte[], but there is also an overload which transforms string -> string. The security given by the two methods is similar; the developer should select whichever overload is most easy for the use case. Irrespective of the overload selected, the value returned by the Protect method is now protected and the application can send it to an untrusted client. arning .Net and enroll yourself in ASP.NET training, then CRB Tech Solutions would be of help. We update you with the current changes in ASP.Net course.Stay linked to the page of CRB Tech for more technical optimization and other resources.

#141412">Data Protection APIs in Asp.Net


#141412">

#141412">

#141412">Getting Started with the Data Protection APIs


#141412">To be simple, protecting data has the following steps:

#141412">

#141412">1. Create a data protector from a data protection provider.

#141412">2. Call the Protect method with the data you want to protect.

#141412">3. Call the Unprotect method with the data you want to convert into plain text.


#141412">Most frameworks such as ASP.NET or SignalR configures the data protection system and sum it to a service container you approach via dependency injection. The following sample explains configuring a service container for dependency injection and listing the data protection stack, receiving the data protection provider via DI, developing a protector and protecting the unprotected data

#141412">using System;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;

#141412">public class Program
{
public static void Main(string[] args)
{
// add data protection services
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection();
var services = serviceCollection.BuildServiceProvider();

#141412">// create an instance of MyClass using the service provider
var instance = ActivatorUtilities.CreateInstance(services);
instance.RunSample();
}

#141412">public class MyClass
{
IDataProtector _protector;

#141412">// the ‘provider’ parameter is provided by DI
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector(“Contoso.MyClass.v1″);
}

#141412">public void RunSample()
{
Console.Write(“Enter input: “);
string input = Console.ReadLine();

#141412">// protect the payload
string protectedPayload = _protector.Protect(input);
Console.WriteLine($”Protect returned: {protectedPayload}”);

#141412">

#141412al">// unprotect the payload
string unprotectedPayload = _protector.Unprotect(protectedPayload);
Console.WriteLine($”Unprotect returned: {unprotectedPayload}”);
}
}
}

#141412">/*
* SAMPLE OUTPUT
*
* Enter input: Hello world!
* Protect returned: CfDJ8ICcgQwZZhlAlTZT…OdfH66i1PnGmpCR5e441xQ
* Unprotect returned: Hello world!


#141412">When you design a protector you should provide one or more Purpose Strings. A purpose string gives isolation between consumers, for instance, a protector designed with a purpose string of “green” would not be able to unprotect data provided by a protector with a purpose of “purple”.

#141412">Examples of IDataProtectionProvider and IDataProtector are thread-safe for many callers. It is said that once a component gets a reference to an IDataProtector via a call to CreateProtector, it will use that reference for multi calls to Protect and Unprotect.

#141412">

#141412">IDataProtectionProvider

#141412">Now the provider interface is the root of the data protection system. It cannot be directly used to protect or unprotect data. In spite, the consumer must get a reference to an IDataProtector by calling IDataProtectionProvider.CreateProtector(purpose), where the purpose is a string that defines the intended consumer use case.


#141412">IDataProtector

#141412">This protector interface is returned by a call to CreateProtector, and it is this interface which consumers can utilize to do protect and unprotect operations.


#141412">To protect a piece of data, pass the data to the Protect method. The basic interface explains a method which transforms byte[] -> byte[], but there is also an overload which transforms string -> string. The security given by the two methods is similar; the developer should select whichever overload is most easy for the use case. Irrespective of the overload selected, the value returned by the Protect method is now protected and the application can send it to an untrusted client.

#141412">arning .Net and enroll yourself in ASP.NET training, then CRB Tech Solutions would be of help. We update you with the current changes in ASP.Net course.


#141412">Stay linked to the page of #bc360aal">CRB Tech #bc360atyle="font-variant: normal"> #141412for more technical optimization and other resources.