18

I've referred the file System.Security.dll as described in this article but according to my IDE, the line using System.Security.Cryptography; can be safely removed as it's not being used.

The same IDE tells me that I've got an error with DataProtectionScope and ProtectedData. Those are supposed to be in that namespace. However, when I dot my way through the packages, I can't see them in there.

Is the article wrong? How can I access the two classes?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • 2
    That should work. What version of .NET Framework are you using? For which platform? How to you make the reference? PS: what's `DataProtextScope`? – Simon Mourier Oct 28 '12 at 17:18
  • 1
    How are we supposed to reproduce this without your code? – CodesInChaos Oct 28 '12 at 17:50
  • 2
    @CodesInChaos What code? I'm typing in `System.Security.Cryptography.` and in the list I'm getting, there's no `DataProtectionScope` or `ProtectedData`. SO, I guess the code is: `System.Security.Cryptography.` but that's hardly a new information. –  Oct 29 '12 at 07:04
  • @SimonMourier VS10, .NET4. I added the reference the usual way, by right-clicking on references in the pane to the right and pointed to the assembly in question. `DataProtextScope` is a typo - see the correction in the subject. :) –  Oct 29 '12 at 07:07
  • 2
    That should definitely work if you see the System.Security assembly listed under the project's References folder. – Simon Mourier Oct 29 '12 at 08:09
  • So what does appear in the list when you type in `System.Security.Cryptography.`? I ask because I tried this on my machine and could see both `DataProtectionScope` and `ProtectedData`. – nick_w Oct 30 '12 at 02:48
  • It works fine. My guess? A code 18. Soo... I doubt anyone could find what's wrong without getting his hands in your solution. – LightStriker Oct 30 '12 at 21:01
  • 2
    These days you can get this assembly from NuGet: https://www.nuget.org/packages/System.Security.Cryptography.ProtectedData/ – JohnLBevan Nov 01 '18 at 11:39

7 Answers7

77

As @Coral Doe mentioned in a comment under @Dave Lucre:

"Had a similar problem and this worked. using System.Security.Cryptography; didn't [show] me [ProtectedData] and ProtectedMemory until I had referenced the System.Security.dll for the specific framework."

This fixed the issue for me. Specifically, I performed these steps:

  1. Open the project in Visual Studio.
  2. Right-click the project's name in Solution Explorer then choose Properties.
  3. Click the References tab on the left.
  4. Click the Add button.
  5. Click the .NET tab.
  6. Select System.Security and click OK to add the reference.

Hope this helps.

George Brian
  • 771
  • 5
  • 2
  • 6
    This should be the answer. I keep forgetting to do this. – valdetero Mar 03 '15 at 23:13
  • 1
    upvote for not having me reinstall the entire visual studio – yitzih Feb 22 '17 at 03:36
  • @George Brian, same thing happened to me (https://stackoverflow.com/questions/46205937/cant-run-npm-install-without-deleting-node-modules-after-upgrading-to-npm-5-4/46393633#46393633). these two are part of a voting ring probably. – Kalana Demel Sep 25 '17 at 03:12
  • 1
    Works for me in .NET 4.6.2 but not in .NET Core. – thatWiseGuy Jul 14 '18 at 21:35
  • This alone didn't help me with VS 2019. In addition I had to go Project>Add Reference>Assemblies and check System.Security. – AlexVB May 14 '20 at 20:28
9

I have referenced the System.Security.dll here: C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Security.dll

Added using System.Security.Cryptography; and I can see both DataProtectionScope and ProtectedData.

I'm targeting the .net 4.0 full framework (not client profile).

What framework are you targeting?

Dave Lucre
  • 1,105
  • 1
  • 14
  • 16
  • 4
    Had a similar problem and this worked. `using System.Security.Cryptography;` didn't showed me `ProtectdData` and `ProtectedMemory` until I had referenced the `System.Security.dll` for the specific framework. – Coral Doe Apr 04 '13 at 09:54
5

I had to add this NuGet package in addition to System.Security.Cryptography.

It is https://www.nuget.org/packages/System.Security.Cryptography.ProtectedData/.

After that, System.Security.Cryptography became "used".

double-beep
  • 5,031
  • 17
  • 33
  • 41
Rob McFeely
  • 2,823
  • 8
  • 33
  • 50
1

As @Dave Lucre asked, what framework is your application targeting? Visual Studio creates all new applications to use the Client Profile by default (which is asinine in my opinion), and it causes all sorts of trouble if you don't specifically change it back. Give that a look, then see where we go from there.

theMayer
  • 15,456
  • 7
  • 58
  • 90
  • I agree that the choice of the primary target platform to Client Profile is borderline insane. I've checked that as well. I don't have those classes anywhere. –  Nov 03 '12 at 01:22
0

Remove references System.Security.Cryptography and add it again.

It works in my case.

4b0
  • 21,981
  • 30
  • 95
  • 142
al.al.se
  • 9
  • 2
0

I'm sorry, I know that this is a quite old post, but I'm having this issue now and the only workaround that I've found is:

  1. Follow the steps mentioned in George Brian's answer
  2. Use System.Security.Cryptography.DataProtectionScope scope = System.Security.Cryptography.DataProtectionScope.LocalMachine; instead of DataProtectionScope scope = DataProtectionScope.LocalMachine;
Mariela
  • 134
  • 2
  • 13
-10

This might be a slap in your face because you've probably tried that (and you haven't, you might deserve one). What happens if you reinstall the whole IDE? And I meant really the whole thing. Remove the framwork, the VS, remove the files, go paranoid and clean up the trash can, switch to a different installation directory etc. Does the problem still remains?

I know it doesn't explain why it happened but at this stage I'm guessing you're mostly interested in how to kill the problem.

If you have access to a spare computer - why don't you install VS on that machine and see if you can reproduce the error. If not, then there's something fishy with your primary machine and you'll probably never know what happened.

I'm stressing here that it's only a list of general suggestions that every programmer should rely on when nothing sane nor logical seems to work. And yes, I do hate computers. I love programming but I hate computers. They are like small, evil people with keyboards...

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • I really, truly don't want to reinstall those things. However, I even trulier don't want to develop data encryptors myself... I'll give it a whack, starting with the least inconvenient one... –  Nov 03 '12 at 01:25
  • 1
    This is very frustrating - the problem **is** gone but I have no idea why... Or how it came up. What do you guys think? –  Nov 03 '12 at 01:54
  • The problem is gone because you eliminated the cause of the problem. I'm guessing you'd like to know **what** that cause was. You're probably not going to know, ever. Especially if you can't reproduce the behavior. I'm glad the issue is resolved, at least. Also, I think the other replies came much sooner and perhaps one of them deserves the +50 more than I. You might consider switching the bounty to someone else. :) – Konrad Viltersten Nov 03 '12 at 10:54