AspNetCore.Docs/aspnetcore/security/data-protection/using-data-protection.md

35 lines
2.4 KiB
Markdown
Raw Normal View History

2016-10-29 01:35:15 +08:00
---
title: Get started with the Data Protection APIs in ASP.NET Core
2016-10-29 01:35:15 +08:00
author: rick-anderson
description: Learn how to use the ASP.NET Core data protection APIs for protecting and unprotecting data in an app.
2016-10-29 01:35:15 +08:00
manager: wpickett
2018-01-29 23:21:31 +08:00
ms.author: riande
2016-10-29 01:35:15 +08:00
ms.date: 10/14/2016
ms.prod: asp.net-core
2018-01-29 23:21:31 +08:00
ms.technology: aspnet
ms.topic: article
2016-10-29 01:35:15 +08:00
uid: security/data-protection/using-data-protection
---
# Get started with the Data Protection APIs in ASP.NET Core
2016-10-29 01:35:15 +08:00
2017-10-14 04:50:30 +08:00
<a name="security-data-protection-getting-started"></a>
2016-10-29 01:35:15 +08:00
At its simplest, protecting data consists of the following steps:
2016-10-29 01:35:15 +08:00
1. Create a data protector from a data protection provider.
2. Call the `Protect` method with the data you want to protect.
2016-10-29 01:35:15 +08:00
3. Call the `Unprotect` method with the data you want to turn back into plain text.
2016-10-29 01:35:15 +08:00
Most frameworks and app models, such as ASP.NET or SignalR, already configure the data protection system and add it to a service container you access via dependency injection. The following sample demonstrates configuring a service container for dependency injection and registering the data protection stack, receiving the data protection provider via DI, creating a protector and protecting then unprotecting data
2016-10-29 01:35:15 +08:00
[!code-csharp[](../../security/data-protection/using-data-protection/samples/protectunprotect.cs?highlight=26,34,35,36,37,38,39,40)]
2016-10-29 01:35:15 +08:00
When you create a protector you must provide one or more [Purpose Strings](xref:security/data-protection/consumer-apis/purpose-strings). A purpose string provides isolation between consumers. For example, a protector created with a purpose string of "green" wouldn't be able to unprotect data provided by a protector with a purpose of "purple".
2016-10-29 01:35:15 +08:00
>[!TIP]
> Instances of `IDataProtectionProvider` and `IDataProtector` are thread-safe for multiple callers. It's intended that once a component gets a reference to an `IDataProtector` via a call to `CreateProtector`, it will use that reference for multiple calls to `Protect` and `Unprotect`.
2016-10-29 01:35:15 +08:00
>
>A call to `Unprotect` will throw CryptographicException if the protected payload cannot be verified or deciphered. Some components may wish to ignore errors during unprotect operations; a component which reads authentication cookies might handle this error and treat the request as if it had no cookie at all rather than fail the request outright. Components which want this behavior should specifically catch CryptographicException instead of swallowing all exceptions.