Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. One more security question

One more security question

Scheduled Pinned Locked Moved C#
questionsecurityhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MKlucher
    wrote on last edited by
    #1

    Still trying to wrap my head around this security stuff, I still can't seem to find a simple way to find out if I have the needed permission when running my program before I call the code that would cause the error. What I would like to do is just see if I have access at the begining of my program and if not pop up a message box and exit. I am trying using the Demand but that seems to be the wrong usage, I also tried using the IsUnrestricted() but that doesn't even seem to be true when running locally. FileIOPermission canAccess = new FileIOPermission(FileIOPermissionAccess.Write, "C:\\"); try { canAccess.Demand() // Keep running through program. } catch { MessageBox.Show "Don't have permissions"; } Is there another way to go about this?

    H 1 Reply Last reply
    0
    • M MKlucher

      Still trying to wrap my head around this security stuff, I still can't seem to find a simple way to find out if I have the needed permission when running my program before I call the code that would cause the error. What I would like to do is just see if I have access at the begining of my program and if not pop up a message box and exit. I am trying using the Demand but that seems to be the wrong usage, I also tried using the IsUnrestricted() but that doesn't even seem to be true when running locally. FileIOPermission canAccess = new FileIOPermission(FileIOPermissionAccess.Write, "C:\\"); try { canAccess.Demand() // Keep running through program. } catch { MessageBox.Show "Don't have permissions"; } Is there another way to go about this?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You don't need to check, really. An exception will be thrown if you don't have the permission. Also, you can't demand a permission if you don't have it (it's more of a check, really). Such checks are handy when you want to know ahead of time if you can do something or not. If you need that permission, it must be granted by a code group with an associated permission set (either custom or one of the pre-configured sets, like FullTrust), and the evidence gathered for your assembly must match the membership condition used for that code group (otherwise the code group policy isn't applied and your assembly (/assemblies) aren't granted the permission set you'd expect. You've got the right idea, though.

      Microsoft MVP, Visual C# My Articles

      M 1 Reply Last reply
      0
      • H Heath Stewart

        You don't need to check, really. An exception will be thrown if you don't have the permission. Also, you can't demand a permission if you don't have it (it's more of a check, really). Such checks are handy when you want to know ahead of time if you can do something or not. If you need that permission, it must be granted by a code group with an associated permission set (either custom or one of the pre-configured sets, like FullTrust), and the evidence gathered for your assembly must match the membership condition used for that code group (otherwise the code group policy isn't applied and your assembly (/assemblies) aren't granted the permission set you'd expect. You've got the right idea, though.

        Microsoft MVP, Visual C# My Articles

        M Offline
        M Offline
        MKlucher
        wrote on last edited by
        #3

        Thats what is getting me confused, The .Demand doesn't throw an exception but when I actually try to modify the file then it throws a security exception. This is some of the test code I have in my program now (that's to test stuff) FileIOPermission canAccess = new FileIOPermission(FileIOPermissionAccess.Write, "C:\\"); try { canAccess.Demand(); File.Delete(cmdArgs[0]); // This contains a file on C } catch { // Let user know here } I would expect the .Demand() call to throw the exception but it doesn't, I get the security error when calling File.Delete, which doesn't make sense... (I also tried replacing Write with AllAccess) The reason I want to run a "pre-check" like this is the delete/renaming stuff doesn't occur until 15 seconds or so in the program so I don't want to make the user wait 15 seconds just to find out that they can't do anything because the code isn't running with the proper permissions. P.S. The SecurityException message that is thrown is: "Request for the permission of type System.Security.Permissions.FileIOPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed."

        H 1 Reply Last reply
        0
        • M MKlucher

          Thats what is getting me confused, The .Demand doesn't throw an exception but when I actually try to modify the file then it throws a security exception. This is some of the test code I have in my program now (that's to test stuff) FileIOPermission canAccess = new FileIOPermission(FileIOPermissionAccess.Write, "C:\\"); try { canAccess.Demand(); File.Delete(cmdArgs[0]); // This contains a file on C } catch { // Let user know here } I would expect the .Demand() call to throw the exception but it doesn't, I get the security error when calling File.Delete, which doesn't make sense... (I also tried replacing Write with AllAccess) The reason I want to run a "pre-check" like this is the delete/renaming stuff doesn't occur until 15 seconds or so in the program so I don't want to make the user wait 15 seconds just to find out that they can't do anything because the code isn't running with the proper permissions. P.S. The SecurityException message that is thrown is: "Request for the permission of type System.Security.Permissions.FileIOPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed."

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          That's a good scenario for a "pre-check"! :) The problem is that, as the documentation states, Demand checks callers higher in the stack. While all callers higher in the stack may have the permission, you may not. So, lets say you add this code in an event handler you've assigned to an event defined in the BCL assemblies (like handling Button.Click or something). Invoking the delegate (your handler) happens from the BCL. Those assemblies would have FullTrust permissions, but you might not. Now, when you simply call File.Delete, you become the caller and don't have that permission, so the SecurityException is thrown. A simple work-around is to attempt to create a dummy file (and delete it, if successful). If you can't write the file, you won't be able to delete a file (well, at least from the CLR's perspective).

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups