ASP.NET Core 中,数据保护主要是用来给服务端设计的,用来替换ASP.NET 1.x-4.x中的,machineKey主要是用来保证使用Form身份验证时Cookie数据的加密解密,以确保不会被修改。或者ViewState数据的加密解密不被篡改,以及对session状态标识进行验证。
先看一下最简单的使用方法:
using System;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
public class Program
{
public static void Main(string[] args)
{
// 添加数据保护到服务中
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection();
var services = serviceCollection.BuildServiceProvider();
// 从DI中创建一个MyClass的实例
var instance = ActivatorUtilities.CreateInstance<MyClass>(services);
instance.RunSample();
}
public class MyClass
{
IDataProtector _protector;
// 参数 'provider' 来自 DI
public MyClass(IDataProtectionProvider provider)
{
_protector = provider.CreateProtector("Contoso.MyClass.v1");
}
public void RunSample()
{
Console.Write("Enter input: ");
string input = Console.ReadLine();
// 加密
string protectedPayload = _protector.Protect(input);
Console.WriteLine($"Protect returned: {protectedPayload}");
// 解密
string unprotectedPayload = _protector.Unprotect(protectedPayload);
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
}
}
}
/*
* 输出:
*
* Enter input: Hello world!
* Protect returned: CfDJ8ICcgQwZZhlAlTZT...OdfH66i1PnGmpCR5e441xQ
* Unprotect returned: Hello world!
*/
在CreateProtector("Contoso.MyClass.v1")中,参数“Contoso.MyClass.v1”可以理解为一个公钥,因为 ASP.NET Core Data Protection 是非对称加密(见前面介绍),所以系统中应该还有一个密钥,那么此处的密钥 ASP.NET Core 在系统内部帮你维护了。
读到这里,有同学可能会问了,那系统中是如何帮我维护我的密钥的呢? 我们不妨先来做一个测试。
首先,我在我的开发环境中,先把上面的程序中的解密部分代码注释掉,然后运行上面的程序,输入一个“Hello World!” ,得到了一个加密的字符串CfDJ8ICcgQwZZhlAlTZT...OdfH66i1PnGmpCR5e441xQ(略写)。
然后我把同样的程序拷贝到另外一台开发环境的机器上,然后把上面的加密部分代码注释掉,使用第一步生成的CfDJ8ICcgQwZZhlAlTZT...OdfH66i1PnGmpCR5e441xQ来解密,注意这两步中我们都使用 "Contoso.MyClass.v1" 来做为公钥。
运行程序,查看结果:

程序抛出了一个“System.Security.Cryptography.CryptographicException”异常的结果。








