编译器错误消息:CS0103:名称“ProtectedData”在当前上下文中不存在 [英] Compiler Error Message: CS0103: The name 'ProtectedData' does not exist in the current context
问题描述
我尝试使用System.Security的加密代码时遇到运行时错误。我添加了一个引用System.Security和一切看起来不错,但我得到这个错误:编译器错误消息:CS0103:名称'ProtectedData'不存在当前上下文
这里是抛出错误的代码。
public static string EncryptString(SecureString input,string entropy)
{
byte [] salt = Encoding.Unicode.GetBytes(entropy);
byte [] encryptedData = ProtectedData.Protect(
Encoding.Unicode.GetBytes(ToInsecureString(input)),
salt,
DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encryptedData);
}
感谢您,
Sam
<您需要为
System.Security.Cryptography
添加一个using语句,并且您需要一个对<$ div> System.Security.dll 。
而不是using语句,你也可以像这样完全限定引用:
byte [] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
Encoding.Unicode.GetBytes(ToInsecureString ,salt,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
I am getting a runtime error when trying to use System.Security's crypto code. I added a reference to System.Security and everything looks good but I am getting this error: "Compiler Error Message: CS0103: The name 'ProtectedData' does not exist in the current context"
here is the code that is throwing the error.
public static string EncryptString(SecureString input, string entropy)
{
byte[] salt = Encoding.Unicode.GetBytes(entropy);
byte[] encryptedData = ProtectedData.Protect(
Encoding.Unicode.GetBytes(ToInsecureString(input)),
salt,
DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encryptedData);
}
Thanks, Sam
You need to add a using statement for System.Security.Cryptography
and you need a reference to System.Security.dll
. From your question it appears you just added the reference and not the using statement.
Instead of the using statement you can also fully qualify the references like this:
byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
Encoding.Unicode.GetBytes(ToInsecureString(input)), salt,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
这篇关于编译器错误消息:CS0103:名称“ProtectedData”在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!