SlideShare a Scribd company logo
Data Protection APIs In Asp.Net
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 services
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection();
var services = serviceCollection.BuildServiceProvider();
// create an instance of MyClass using the service provider
var instance = ActivatorUtilities.CreateInstance<MyClass>(services);
instance.RunSample();
}
public class MyClass
{
IDataProtector _protector;
// the 'provider' parameter is provided by DI
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("Contoso.MyClass.v1");
}
public void RunSample()
{
Console.Write("Enter input: ");
string input = Console.ReadLine();
// protect the payload
string protectedPayload = _protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayload}");
// unprotect the payload
string 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.
A call to Unprotect will throw CryptographicException if the protected payload cannot
be evaluated. Some components might wish to ignore errors during unprotect operations;
a component which reads genuine cookies might handle this error and treat the request
as if it had no cookie at all rather than fail the request.
An overview of consumer APIs
The IDataProtectionProvider and IDataProtector interfaces are the fundamental
interfaces via which consumers use the data protection method. They are located in the
Microsoft.AspNetCore.DataProtection.Interfaces.
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.
In order to unprotect a previously-protected piece of data, then pass the protected data to
the Unprotect method. If the covered payload was produced by an earlier call to Protect
on this same IDataProtector, Unprotect method will give the original unprotected
payload. If the protected payload has been interfered with or was yielded by a different
IDataProtector, the Unprotect method will give CryptographicException.
If you are interested in learning .Net and enroll yourself in ASP.NET training, then
CRB Tech Solutions would be of help. We update ourself with the current changes in
ASP.Net course.
Stay linked to the page of CRB Tech reviews for more technical optimization and other
resources.

More Related Content

PDF
Database security issues
PPTX
Privacy preserving public auditing for regenerating-code-based cloud storage
PDF
Multi-part Dynamic Key Generation For Secure Data Encryption
DOC
Oruta privacy preserving public auditing for shared data in the cloud
PPTX
Security of the database
DOCX
Audit free cloud storage via deniable attribute based encryption
PDF
GreenSQL Security
PDF
Control cloud data access privilege and anonymity with fully anonymous attri...
Database security issues
Privacy preserving public auditing for regenerating-code-based cloud storage
Multi-part Dynamic Key Generation For Secure Data Encryption
Oruta privacy preserving public auditing for shared data in the cloud
Security of the database
Audit free cloud storage via deniable attribute based encryption
GreenSQL Security
Control cloud data access privilege and anonymity with fully anonymous attri...

What's hot (20)

PPTX
A New Mode to Ensure Security in Cloud Computing Services
PDF
Database security
DOCX
identity based encryption transformation for flexible sharing of encrypted da...
PPTX
Decentralized access control with authentication anonymous of data stored in ...
PPTX
Attribute-Based Encryption for Cloud Security
PPTX
DBMS SECURITY
PDF
IRJET- An Efficient Ranked Multi-Keyword Search for Multiple Data Owners Over...
PPTX
A Fully Anonymous Attribute-Based Encryption to Control Cloud Data Access and...
PPTX
Decentralized access control with anonymous authentication of data stored in ...
PDF
A robust and verifiable threshold multi authority access control system in pu...
DOCX
Decentralized access control with anonymous authentication of data stored in ...
PDF
Secure Sharing PHI PCI PII -Android app_Content Provider
PPT
Database Systems Security
PPTX
Database Security
DOC
Decentralized access control with anonymous authentication of data stored in ...
DOCX
Decentralized access control with anonymous authentication of data stored in ...
PPTX
Secure cloud storage
PDF
Securely Data Forwarding and Maintaining Reliability of Data in Cloud Computing
PPTX
01 database security ent-db
DOCX
Oruta privacy preserving public auditing for shared data in the cloud
A New Mode to Ensure Security in Cloud Computing Services
Database security
identity based encryption transformation for flexible sharing of encrypted da...
Decentralized access control with authentication anonymous of data stored in ...
Attribute-Based Encryption for Cloud Security
DBMS SECURITY
IRJET- An Efficient Ranked Multi-Keyword Search for Multiple Data Owners Over...
A Fully Anonymous Attribute-Based Encryption to Control Cloud Data Access and...
Decentralized access control with anonymous authentication of data stored in ...
A robust and verifiable threshold multi authority access control system in pu...
Decentralized access control with anonymous authentication of data stored in ...
Secure Sharing PHI PCI PII -Android app_Content Provider
Database Systems Security
Database Security
Decentralized access control with anonymous authentication of data stored in ...
Decentralized access control with anonymous authentication of data stored in ...
Secure cloud storage
Securely Data Forwarding and Maintaining Reliability of Data in Cloud Computing
01 database security ent-db
Oruta privacy preserving public auditing for shared data in the cloud
Ad

Viewers also liked (12)

DOCX
Wanghinggarm10 3-111211095017-phpapp02
PPTX
Mapa mental de gestion de conocimiento
PDF
Chi cuadarado ni tan cuadrado
PDF
PDF
Thalys Sommerfahrplan 2015
DOCX
REAL ESTATE HOT SPOT IN CHENNAI.
DOCX
MY COVER LETTER (2)
PDF
Conmutacion
DOC
2012 1 plano-referencia_segunda feira
ODP
Ejercicios Ficheros y Directorios
PPTX
New house plans for February 2016
PPTX
House plans for July - August 2016
Wanghinggarm10 3-111211095017-phpapp02
Mapa mental de gestion de conocimiento
Chi cuadarado ni tan cuadrado
Thalys Sommerfahrplan 2015
REAL ESTATE HOT SPOT IN CHENNAI.
MY COVER LETTER (2)
Conmutacion
2012 1 plano-referencia_segunda feira
Ejercicios Ficheros y Directorios
New house plans for February 2016
House plans for July - August 2016
Ad

Similar to Data protection api's in asp dot net (15)

PPTX
Securing your azure web app with asp.net core data protection
PDF
How to not shoot yourself in the foot when working with serialization
PPT
C++ classes tutorials
DOC
7 latest-dot-net-interview-questions
DOC
136 latest dot net interview questions
PPTX
BlueHat v17 || Dangerous Contents - Securing .Net Deserialization
PDF
Joel Landis Net Portfolio
PPT
How to ace your .NET technical interview :: .Net Technical Check Tuneup
PPT
Basic c#
PPTX
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
DOCX
C# concepts
DOC
Serialization in .NET
PDF
.NET Portfolio
PPTX
Dependency Injection in .NET
PPTX
Polaris presentation ioc - code conference
Securing your azure web app with asp.net core data protection
How to not shoot yourself in the foot when working with serialization
C++ classes tutorials
7 latest-dot-net-interview-questions
136 latest dot net interview questions
BlueHat v17 || Dangerous Contents - Securing .Net Deserialization
Joel Landis Net Portfolio
How to ace your .NET technical interview :: .Net Technical Check Tuneup
Basic c#
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
C# concepts
Serialization in .NET
.NET Portfolio
Dependency Injection in .NET
Polaris presentation ioc - code conference

More from sonia merchant (20)

PPT
What does dot net hold for 2016?
PDF
What does .net hold for 2016?
PDF
Authorization p iv
PDF
Authorization iii
PDF
Authorization in asp dot net part 2
PDF
Asp dot-net core problems and fixes
PPTX
Search page-with-elasticsearch-and-dot-net
PPTX
Build a-search-page-with-elastic search-and-dot-net
PDF
How to optimize asp dot-net application
PPT
How to optimize asp dot net application ?
PDF
10 things to remember
PPT
Learn dot net attributes
PDF
Learn about dot net attributes
PPTX
Owin and-katana-overview
PPTX
Top 15-asp-dot-net-interview-questions-and-answers
PPT
Next generation asp.net v next
PPT
Dot net universal apps
PDF
Browser frame building with c# and vb dot net
PPTX
A simplest-way-to-reconstruct-.net-framework
PDF
Silverlight versions-features
What does dot net hold for 2016?
What does .net hold for 2016?
Authorization p iv
Authorization iii
Authorization in asp dot net part 2
Asp dot-net core problems and fixes
Search page-with-elasticsearch-and-dot-net
Build a-search-page-with-elastic search-and-dot-net
How to optimize asp dot-net application
How to optimize asp dot net application ?
10 things to remember
Learn dot net attributes
Learn about dot net attributes
Owin and-katana-overview
Top 15-asp-dot-net-interview-questions-and-answers
Next generation asp.net v next
Dot net universal apps
Browser frame building with c# and vb dot net
A simplest-way-to-reconstruct-.net-framework
Silverlight versions-features

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Presentation on HIE in infants and its manifestations
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
O7-L3 Supply Chain Operations - ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
01-Introduction-to-Information-Management.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial diseases, their pathogenesis and prophylaxis
Presentation on HIE in infants and its manifestations
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Data protection api's in asp dot net

  • 1. Data Protection APIs In Asp.Net 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
  • 2. using System; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.DependencyInjection; public class Program { public static void Main(string[] args) { // add data protection services var serviceCollection = new ServiceCollection(); serviceCollection.AddDataProtection(); var services = serviceCollection.BuildServiceProvider(); // create an instance of MyClass using the service provider var instance = ActivatorUtilities.CreateInstance<MyClass>(services); instance.RunSample(); } public class MyClass { IDataProtector _protector; // the 'provider' parameter is provided by DI public MyClass(IDataProtectionProvider provider)
  • 3. { _protector = provider.CreateProtector("Contoso.MyClass.v1"); } public void RunSample() { Console.Write("Enter input: "); string input = Console.ReadLine(); // protect the payload string protectedPayload = _protector.Protect(input); Console.WriteLine($"Protect returned: {protectedPayload}"); // unprotect the payload string unprotectedPayload = _protector.Unprotect(protectedPayload); Console.WriteLine($"Unprotect returned: {unprotectedPayload}"); } } } /* * SAMPLE OUTPUT * * Enter input: Hello world! * Protect returned: CfDJ8ICcgQwZZhlAlTZT...OdfH66i1PnGmpCR5e441xQ * Unprotect returned: Hello world!
  • 4. 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. A call to Unprotect will throw CryptographicException if the protected payload cannot be evaluated. Some components might wish to ignore errors during unprotect operations; a component which reads genuine cookies might handle this error and treat the request as if it had no cookie at all rather than fail the request. An overview of consumer APIs The IDataProtectionProvider and IDataProtector interfaces are the fundamental interfaces via which consumers use the data protection method. They are located in the Microsoft.AspNetCore.DataProtection.Interfaces. 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
  • 5. the overload selected, the value returned by the Protect method is now protected and the application can send it to an untrusted client. In order to unprotect a previously-protected piece of data, then pass the protected data to the Unprotect method. If the covered payload was produced by an earlier call to Protect on this same IDataProtector, Unprotect method will give the original unprotected payload. If the protected payload has been interfered with or was yielded by a different IDataProtector, the Unprotect method will give CryptographicException. If you are interested in learning .Net and enroll yourself in ASP.NET training, then CRB Tech Solutions would be of help. We update ourself with the current changes in ASP.Net course. Stay linked to the page of CRB Tech reviews for more technical optimization and other resources.