编译器错误信息:CS0103:在当前上下文中不存在名称'ProtectedData'。

5

当我尝试使用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。
2个回答

12

你需要添加一个针对System.Security.Cryptography的using语句,还需要引用System.Security.dll。从你的问题中看来,你只是添加了引用而没有加上using语句。

除了using语句以外,你也可以使用完全限定的引用方式:

byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
            Encoding.Unicode.GetBytes(ToInsecureString(input)), salt,
            System.Security.Cryptography.DataProtectionScope.CurrentUser);

0
在我的情况下,我需要安装NugGet包System.Security.Cryptography.ProtectedData。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接