Compiler Error Message: CS0103: The name 'ProtectedData' does not exist in the current context

本秂侑毒 提交于 2020-01-03 17:45:15

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/18455442/compiler-error-message-cs0103-the-name-protecteddata-does-not-exist-in-the-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!