Don't. Well, don't if you're building an ASP.NET application that's going to run in a walled off environment on a shared server.
Why?
Well, it's an interesting issue of security, you see. Strange isn't it, that the Data Protection API has security issues?
Using the DPAPI requires access to a key container. This is where any keys created by the DPAPI are stored so they can be used to encrypt and decrypt data. There are two types of key containers in the DPAPI. User-level and machine-level. By default, the DPAPI uses a user-level key container.
The user-level key container is stored in the user's profile. When you use the DPAPI for a standard Windows-based (WinForms/WPF etc) application, you won't run into any trouble. When you try to use it in an ASP.NET application you'll hit some hurdles, but you won't notice until you put it into production. Unless you've read this.
The trick here is the fact that the user-level key container is stored in the user's profile. When you're developing an ASP.NET application, the application runs under the logged-in user's profile (yours). There's a key container available, and all the lovely encryption goodies work.
When you put the application into a production environment, regardless of the user context you setup for the worker process group, it will fall apart. The user that ASP.NET is set to run under never logs in. And this is an important point. This means the user profile is never loaded, so you have no user-level key container available to store the keys that you create. You'll find this out thanks to a wonderfully ambiguous and unhelpful exception.
If you're like me (and lets face it, who isn't?) you'll be straight onto Google searching on the error message. You'll track down a lovely knowledge-base article (which I don't have the link for, sorry...) that tells you the simple work around is to specify that the DPAPI should use the machine key store. It's a simple property called something like UseMachineKeyStore (I kid you not) and you set it to true. There's one more thing, and that's a policy permission you'll need to set on the user account the WPG runs under to allow it access to the machine key container.
And therein lies the rub.
Firstly, the machine key store is a machine-wide resource. If an application crashes or is compromised in some way, the crashed app/compromisor has access to a machine level resource that contains some incredibly sensitive information! Not only that, there's the potential attack vector that arises, as well as the ability to abuse the API and cause the machine to crash effecting a denial-of-service.
Secondly, you've had to elevate the permissions of the user account that the application is running under. The idea is to have the least amount of permissions possible for these user accounts to close the door as much as we can. Allowing the user account access to the machine key store is like hanging your underwear on your front porch. Before you've washed it.
Okay, what can I do about that?
Simple. You'll have to stump up for a third party library that will do the same trick for you. Make sure it doesn't just wrap the MS DPAPI, and that it uses standard and verifiable algorithms for the encryption and key generation.
A library I've used to solve this problem is from Chilkat Software. The website looks like a clearance sale ad for ski gear come summer, and I think the licensing mechanism could've been better thought out, but the components themselves are extremely well put together.
You can use a library like this to generate RSA encryption keys and encrypt and decrypt strings that can be read by any other standards following library (including the MS DPAPI).
These components run in-process in the exact same way as any code you've written for the application does. They don't require access to any key containers, or elevated permissions for any users.
Note: I have no association with Chilkat outside the fact that I've bought and used these components.