I created a new console app in VS2017 and I am trying to run this code to demonstrate I can encrypt and decrypt strings in .NET Core. I have tried calling RunSample from the Program.cs Main but it wants it to be a static method. If I make RunSample static then I'm getting a null reference exception when trying to set the var protectedpayload.
using System;
using Microsoft.AspNetCore.DataProtection;
public class MyClass
{
private readonly IDataProtector _protector;
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("Contoso.MyClass.v1");
}
public void RunSample()
{
Console.WriteLine("Enter input:");
string input = Console.ReadLine();
var protectedPayLoad = _protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayLoad}");
var unprotectedPayLoad = _protector.Unprotect(protectedPayLoad);
Console.WriteLine($"Unprotect returned: {unprotectedPayLoad}");
}
}
How can I run it?
UPDATE:
Trying to run it from Program.cs, I have the following but .MyClass has a "cannot resolve" syntax error / red underline on it.
using System;
namespace encrypttest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
new Program().MyClass.RunSample();
}
}
}
UPDATE 2:
I created my app by going to new project, .NET Core and then Console App (.NET Core):

UPDATE 3:
I've changed my code as suggested but have the following error:

UPDATE 4:
I've removed the using as suggested, but now I get:
