52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using System;
|
|
using Microsoft.AspNet.DataProtection;
|
|
using Microsoft.Framework.DependencyInjection;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
// add data protection services
|
|
var serviceCollection = new ServiceCollection();
|
|
serviceCollection.AddDataProtection();
|
|
var services = serviceCollection.BuildServiceProvider();
|
|
|
|
// create an instance of MyClass using the service provider
|
|
var instance = ActivatorUtilities.CreateInstance<MyClass>(services);
|
|
instance.RunSample();
|
|
}
|
|
|
|
public class MyClass
|
|
{
|
|
IDataProtector _protector;
|
|
|
|
// the 'provider' parameter is provided by DI
|
|
public MyClass(IDataProtectionProvider provider)
|
|
{
|
|
_protector = provider.CreateProtector("Contoso.MyClass.v1");
|
|
}
|
|
|
|
public void RunSample()
|
|
{
|
|
Console.Write("Enter input: ");
|
|
string input = Console.ReadLine();
|
|
|
|
// protect the payload
|
|
string protectedPayload = _protector.Protect(input);
|
|
Console.WriteLine($"Protect returned: {protectedPayload}");
|
|
|
|
// unprotect the payload
|
|
string unprotectedPayload = _protector.Unprotect(protectedPayload);
|
|
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* SAMPLE OUTPUT
|
|
*
|
|
* Enter input: Hello world!
|
|
* Protect returned: CfDJ8ICcgQwZZhlAlTZT...OdfH66i1PnGmpCR5e441xQ
|
|
* Unprotect returned: Hello world!
|
|
*/
|