简体   繁体   English

编译器错误消息:CS0103:当前上下文中不存在名称“ProtectedData”

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

I am getting a runtime error when trying to use System.Security's crypto code. 我在尝试使用System.Security的加密代码时遇到运行时错误。 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" 我添加了对System.Security的引用,一切看起来不错,但我收到此错误:“编译器错误消息:CS0103:名称'ProtectedData'在当前上下文中不存在”

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 . 您需要为System.Security.Cryptography添加using语句,并且需要对System.Security.dll的引用。 From your question it appears you just added the reference and not the using statement. 从您的问题看来,您刚刚添加了引用而不是using语句。

Instead of the using statement you can also fully qualify the references like this: 您也可以完全限定引用,而不是using语句,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译器错误消息:CS0103:当前上下文中不存在名称“ listBox1” - Compiler Error Message: CS0103: The name 'listBox1' does not exist in the current context 编译器错误消息:CS0103:名称“绑定”在当前上下文中不存在 - Compiler Error Message: CS0103: The name 'Bind' does not exist in the current context 编译器错误消息:CS0103:在当前上下文中不存在名称“网格” - Compiler Error Message: CS0103: The name 'grid' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误cs0103名称'IEnumerator'在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context 错误CS0103当前上下文中不存在名称“图像” - Error CS0103 The name 'Image' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM