Search code examples
c#asp.netconsole-applicationmachinekey

Machinekey does not exist in the current context


I am trying to learn some cryptography - from scratch - an for this I am trying to test the the protect and unprotect-methods which both contain the MachineKey-class.

But when putting the code inside an asp.net console application, I get the following error

"machinekey does not occur in the current context"

, even when trying to include

using System.Web.Security

What am I doing wrong? I just want to test the two methods and therefore place them in a console application, where I can easily check the output.

snippet of the Protect-method:

                public static string Protect(string unprotectedText)
            {
                var unprotectedBytes = Encoding.UTF8.GetBytes(unprotectedText);
                var protectedBytes = MachineKey.Protect(unprotectedBytes, "Recipient: user");
                var protectedText = Convert.ToBase64String(protectedBytes);
                return protectedText;
            }

Solution

  • I installed the package Microsoft.AspNetCore.DataProtection.SystemWeb.

    Reference: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-2.2#package-layout