C# and VS2015: The name ‘ProtectedData’ does not exist in the current context

I need to use a small encrypter/decrypter for a basic C# application I started develop.

I didn’t need a complete password protection suite, but just a simple way to make it harder to read password stored in a setting file.

Google showed me that .NET provides a built-in encryption method under System.Security.Cryptography suite.

(More Details: MSDN : ProtectedData Class)

So I set out to create a small function like this:

using System.Security.Cryptography;
public static class DotNetEncoder
    {
        public static string Encode(this string text)
        {
            return Convert.ToBase64String(
                ProtectedData.Protect(
                    Encoding.Unicode.GetBytes(text), null, DataProtectionScope.LocalMachine));
        }
 
        public static string Decode(this string text)
        {
            return Encoding.Unicode.GetString(
                ProtectedData.Unprotect(
                    Convert.FromBase64String(text), null, DataProtectionScope.LocalMachine));
        }
}

The only problem was that, Visual Studio thought ProtectedData does not exist under System.Security.Cryptography – although it’s there for sure…

Cause: Visual Studio 2015 sets Target Framework as .NET Framework 4.5.2. With this target, Cryptography functions does not appear to be available.

Solution: You need to ensure that the project’s Target Framework is set to a correct version: I set to 4.6, reloaded the project, and everything was working as expected.

Setting VS2015 Project Properties

 

2 thoughts on “C# and VS2015: The name ‘ProtectedData’ does not exist in the current context

Leave a Reply

Your email address will not be published.