Security.PrincipalPermission.Demand()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeroen

    #1

    Security.PrincipalPermission.Demand()

    Hi,

    Normally my programming style is as follows:

    /*************** */
    if ( SomeObject.CanY ouDoThis() == true)
    SomeObject.DoSt uff();
    else
    MessageBox.Show ("Nope, sowwy...");
    /*************** */

    Meaning, most usually I don't just call "DoStuff()" and show the 'nope
    sowwy' message if I caught an exception.

    Now, the question is: can someone explain why the
    PrincipalPermis sion's Demand method seems to work like this? MS
    recommends to try the Demand() method before you do something secure,
    and catch any exception to handle non-authenticated users. This seems
    a bit like misusing exceptions?

    Thanks.

    -Jeroen
  • Jeroen

    #2
    Re: Security.Princi palPermission.D emand()

    Bump?

    -Jeroen

    Comment

    • Marc Gravell

      #3
      Re: Security.Princi palPermission.D emand()

      MsgBox is fine at the UI, and indeed the UI would be correct to check
      whether something should work before attempting it (by calling
      IsInRole) - however, when you're a few layers down (i.e. not touching
      the UI), the best way to indicate "nope, sowwy" is to throw an
      exception. To me, this is entirely reasonable behavior if the caller
      has failed to enforce the pre-requisites for a method (i.e. that the
      user is in a given role), and is *correctly* using exceptions. Return
      codes are generally simply not a good way of indicating success
      [although it is fine for the bool TrySomething(.. .) signatures].

      In fact, the runtime takes it a level further; you can mark a method
      with the [PrincipalPermis sion(Demand...b lah)] attribute and it will
      enforce it at the point the method is invoked - again, throwing an
      exception as a perfectly well-defined "as if!" response.

      Marc

      Comment

      • Jeroen

        #4
        Re: Security.Princi palPermission.D emand()

        Thanks Marc, that answers my question.

        -Jeroen

        Comment

        Working...