为什么会发生此DataProtection -Provider错误
我正在使用DataProtectionProvider类测试配置数据保护。测试代码以一种方式工作,但以另一种方式失败。
测试环境:
应用程序类型:ASP.NET 6带C#的控制台应用程序 软件包:Microsoft.aspnetcore.dataprotection.extensions 6.0.3 IDE:vs 2022 测试项目:protectData
问题描述:
在DataProtector类中具有3种方法(请参阅测试代码#1)。
TestProtector方法用于初始测试。它以相同的方法加密和解密数据。它没有任何问题
,并在两个单独的步骤中处理该过程。使用这些方法运行测试时,该语句上的dectryptata方法中发生了例外:解密= protector.unprotect(EncryptedData);例外信息如下屏幕截图所示。
使用程序进行测试。
问题:
在将“ testProtector”方法与这两种方法中的代码进行比较时,它们都以相同的方式处理该过程使用相同的密钥。为什么一种方法可以完美地工作,而“两步”总是失败?这真的让我感到困惑。我感谢任何帮助或建议进行故障排除的建议。
测试代码
----测试代码#1(使用dataprotectionprovider的控制台应用)
using Microsoft.AspNetCore.DataProtection;
using System;
namespace ProtectData
{
public static class DataProtector
{
public static string EncryptData(string inputText)
{
string encrypted = string.Empty;
try
{
var dataProtectionProvider = DataProtectionProvider.Create($".\appconfig.txt");
var protector = dataProtectionProvider.CreateProtector("protect data");
//var protectedPayload = protector.Protect(inputText);
encrypted = protector.Protect(inputText);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message);
}
return encrypted;
}
public static string DecryptData(string encryptedData)
{
string decrypted = string.Empty;
try
{
var dataProtectionProvider = DataProtectionProvider.Create($".\appconfig.txt");
var protector = dataProtectionProvider.CreateProtector("protect conn string");
decrypted = protector.Unprotect(encryptedData);
}
catch(Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message, ex);
}
return decrypted;
}
public static void TestProtector()
{
string inputText = "DataSource=localhost, database=testdb, userID=appuser, password=topsecret";
Console.WriteLine($"inputText:\n{inputText}\n");
string encrypted = string.Empty;
string decrypted = string.Empty;
try
{
// encrypt given string
var dataProtectionProvider = DataProtectionProvider.Create($".\appconfig.txt");
var protector = dataProtectionProvider.CreateProtector("protect data");
//generate protected payload for input text
encrypted = protector.Protect(inputText);
Console.WriteLine($"protectedPayload:\n{encrypted}\n");
//decrypt protected data
decrypted = protector.Unprotect(encrypted);
Console.WriteLine($"UnprotectPayload:\n{decrypted}\n");
//show verification result
Console.WriteLine($"Verify result:\n{(inputText == decrypted ? true : false)}");
}
catch(Exception ex)
{
Console.WriteLine("Error:", ex);
}
}
}
}
----测试代码#2(程序主)
namespace ProtectData
{
public class Program
{
static void Main()
{
string testType = "two_step";
RunTest(testType);
Console.WriteLine();
Console.WriteLine("Press any key...");
Console.ReadKey();
}
static void RunTest(string testType)
{
switch ( testType.ToLower())
{
case "simple":
DataProtector.TestProtector();
break;
case "two_step":
string inputData = "DataSource=localhost, database=testdb, userID=appuser, password=topsecret";
Console.WriteLine($"inputData:\n{inputData}\n");
string protectedData = DataProtector.EncryptData(inputData);
Console.WriteLine($"protectedData:\n{protectedData}\n");
string outputData = DataProtector.DecryptData(protectedData);
Console.WriteLine($"outputData:\n{outputData}\n");
bool verify = inputData == outputData;
Console.WriteLine($"verified: {verify}");
break;
}
}
}
}
I am testing the config data protection using DataProtectionProvider class. The test code works in one way, but failed in another way.
TEST ENVIRONMENT:
App type: ASP.NET 6 console app with c#
Package: Microsoft.AspNetCore.DataProtection.Extensions 6.0.3
IDE: VS 2022
Test Project: ProtectData
PROBLEM DESCRIPTION:
In DataProtector class has 3 methods (see test code #1).
The TestProtector method is for initial testing. It encrypt and decrypt data in the same method. It works without any problem
In methods EncryptData and DecryptData handle the process in 2 separate steps. When running tests with these methods, the exception occurs in the DecryptData method on the statement: decrypted = protector.Unprotect(encryptedData); The exception info is shown in following screen shot.
The tests were run using the program.main method (see test code $2)
QUESTION:
When comparing the code in "TestProtector" method with code in these 2 method, they all handle the process in the same way with same key. Why does one method works perfectly and the "two-step" always fail? It really puzzles me. I'll appreciate any help or suggestions for troubleshooting.
TEST CODE
---- test code #1 (console app using DataProtectionProvider)
using Microsoft.AspNetCore.DataProtection;
using System;
namespace ProtectData
{
public static class DataProtector
{
public static string EncryptData(string inputText)
{
string encrypted = string.Empty;
try
{
var dataProtectionProvider = DataProtectionProvider.Create(quot;.\appconfig.txt");
var protector = dataProtectionProvider.CreateProtector("protect data");
//var protectedPayload = protector.Protect(inputText);
encrypted = protector.Protect(inputText);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message);
}
return encrypted;
}
public static string DecryptData(string encryptedData)
{
string decrypted = string.Empty;
try
{
var dataProtectionProvider = DataProtectionProvider.Create(quot;.\appconfig.txt");
var protector = dataProtectionProvider.CreateProtector("protect conn string");
decrypted = protector.Unprotect(encryptedData);
}
catch(Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message, ex);
}
return decrypted;
}
public static void TestProtector()
{
string inputText = "DataSource=localhost, database=testdb, userID=appuser, password=topsecret";
Console.WriteLine(quot;inputText:\n{inputText}\n");
string encrypted = string.Empty;
string decrypted = string.Empty;
try
{
// encrypt given string
var dataProtectionProvider = DataProtectionProvider.Create(quot;.\appconfig.txt");
var protector = dataProtectionProvider.CreateProtector("protect data");
//generate protected payload for input text
encrypted = protector.Protect(inputText);
Console.WriteLine(quot;protectedPayload:\n{encrypted}\n");
//decrypt protected data
decrypted = protector.Unprotect(encrypted);
Console.WriteLine(quot;UnprotectPayload:\n{decrypted}\n");
//show verification result
Console.WriteLine(quot;Verify result:\n{(inputText == decrypted ? true : false)}");
}
catch(Exception ex)
{
Console.WriteLine("Error:", ex);
}
}
}
}
---- Test code #2 (program main)
namespace ProtectData
{
public class Program
{
static void Main()
{
string testType = "two_step";
RunTest(testType);
Console.WriteLine();
Console.WriteLine("Press any key...");
Console.ReadKey();
}
static void RunTest(string testType)
{
switch ( testType.ToLower())
{
case "simple":
DataProtector.TestProtector();
break;
case "two_step":
string inputData = "DataSource=localhost, database=testdb, userID=appuser, password=topsecret";
Console.WriteLine(quot;inputData:\n{inputData}\n");
string protectedData = DataProtector.EncryptData(inputData);
Console.WriteLine(quot;protectedData:\n{protectedData}\n");
string outputData = DataProtector.DecryptData(protectedData);
Console.WriteLine(quot;outputData:\n{outputData}\n");
bool verify = inputData == outputData;
Console.WriteLine(quot;verified: {verify}");
break;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够使用以下在线文档中提供的示例解决问题:
https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/non-di-scenarios?view=aspnetcore-6.0
I was able to fix the problem using examples provided in following online doc:
https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/non-di-scenarios?view=aspnetcore-6.0