Getting noddy CAS example working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • codefragment@googlemail.com

    #1

    Getting noddy CAS example working

    Hi
    I'm stuck with the basics of CAS. I have a solution with two
    projects, Assembly1 and Assembly2. Assembly1 is happily making calls
    to the registry, filesystem, etc

    Assembly1 calls a method on Assembly2, in the AssemblyInfo file for
    Assembly2 I have these lines:

    [assembly: AllowPartiallyT rustedCallers]
    [assembly: FileIOPermissio n(SecurityActio n.RequestMinimu m, Read = @"D:
    \temp\temp.txt" )]
    [assembly: FileIOPermissio n(SecurityActio n.RequestOption al, Read = @"D:
    \temp\temp.txt" )]

    and I have in a method

    FileStream s = File.Open(@"D:\ temp\temp.txt", FileMode.Open);

    When I run this I get an error on that line which says
    "Request for the permission of type
    System.Security .Permissions.Fi leIOPermission (etc) failed]

    If I remove the AssemblyInfo lines and just add this
    [assembly: FileIOPermissio n(SecurityActio n.RequestOption al, Read = @"D:
    \temp\temp.txt" , Unrestricted = true)]

    then it works. I don't understand.
    (1) Why do I need unrestricted access? I just want to read the file
    (2) I though if you use RequestOptional it meant that you also needed
    AllowPartiallyT rustedCallers for any calling assembly

    please help, spent hours on this and I haven't a clue
  • codefragment@googlemail.com

    #2
    Re: Getting noddy CAS example working

    k, I assume its because I did:

    FileStream s = File.Open(@"D:\ temp\temp.txt", FileMode.Open);

    and File.Open can do more than read, if I do this in the assembly:

    [assembly: FileIOPermissio n(SecurityActio n.RequestMinimu m,All = @"D:
    \temp\temp.txt" )]
    [assembly: FileIOPermissio n(SecurityActio n.RequestOption al, All = @"D:
    \temp\temp.txt" )]

    it works, replacing Read with All, or similiarly this (which to my
    mind is clearer)

    [assembly: FileIOPermissio n(SecurityActio n.RequestMinimu m,All = @"D:
    \temp\temp.txt" )]
    [assembly: PermissionSet(S ecurityAction.R equestOptional)]

    Comment

    • Alberto Poblacion

      #3
      Re: Getting noddy CAS example working

      <codefragment@g ooglemail.comwr ote in message
      news:714c36c7-7801-4445-b884-511681ac9da4@m7 3g2000hsh.googl egroups.com...
      I'm stuck with the basics of CAS. I have a solution with two
      projects, Assembly1 and Assembly2. Assembly1 is happily making calls
      to the registry, filesystem, etc
      >
      Assembly1 calls a method on Assembly2, in the AssemblyInfo file for
      Assembly2 I have these lines:
      >
      [assembly: AllowPartiallyT rustedCallers]
      [assembly: FileIOPermissio n(SecurityActio n.RequestMinimu m, Read = @"D:
      \temp\temp.txt" )]
      [assembly: FileIOPermissio n(SecurityActio n.RequestOption al, Read = @"D:
      \temp\temp.txt" )]
      >
      and I have in a method
      >
      FileStream s = File.Open(@"D:\ temp\temp.txt", FileMode.Open);
      >
      When I run this I get an error on that line which says
      "Request for the permission of type
      System.Security .Permissions.Fi leIOPermission (etc) failed]
      >
      If I remove the AssemblyInfo lines and just add this
      [assembly: FileIOPermissio n(SecurityActio n.RequestOption al, Read = @"D:
      \temp\temp.txt" , Unrestricted = true)]
      >
      then it works. I don't understand.
      (1) Why do I need unrestricted access? I just want to read the file
      (2) I though if you use RequestOptional it meant that you also needed
      AllowPartiallyT rustedCallers for any calling assembly
      >
      please help, spent hours on this and I haven't a clue
      (1) The problem is in the line
      FileStream s = File.Open(@"D:\ temp\temp.txt", FileMode.Open);
      This opens the file for reading AND WRITING, even if your code desn't
      write anything to the Stream that you opened. So in the moment of executing
      that statement you need read and write permission, which is granted by the
      "unrestrict ed" permission request, but not by the "read" request.

      (2) I understand that AllowPartiallyT rustedCallers is only needed in the
      called assembly if it has a strong name and if it is being called from a
      calling assembly that does not have Full Trust.

      Comment

      • codefragment@googlemail.com

        #4
        Re: Getting noddy CAS example working

        Your reply came 2 minutes after my post :-) But thanks, if I hadn't
        found it your answer would have been enough for me to sort it

        thanks

        Comment

        Working...