Security Attributes without Try-Finally?

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

    Security Attributes without Try-Finally?


    I have decorated several classes and methods in an
    ASP.NET appliation with declarative security attributes
    for roles. For example:

    [System.Security .Permissions.Pr incipalPermissi on
    (System.Securit y.Permissions.S ecurityAction.D emand ,
    Role="SomeRole" )]

    I currently use a Try...Finally block in calling code to
    test a user's Role permissions. I would like to get away
    from this and use a real logical construct.

    How can I test for Role access with attributes and not
    use Try...Finally?

    Thanks.
  • Steven Cheng[MSFT]

    #2
    RE: Security Attributes without Try-Finally?

    Hi localhost,


    Thank you for using Microsoft Newsgroup Sevice. Based on your description,
    you are wanting to apply Role-based access checking in the some methods,
    also you don't want to use the "Try ... catch ...Finally" style to check.
    Is my understanding of'
    your problem correct?

    If so, here is some suggestions on it:

    If you do not want a thrown exception to be the default behavior for
    validation failure. In this case, you can use the static CurrentPrincipa l
    property on the System.Threadin g.Thread class to access the Principal
    object and call its methods.

    After obtaining the principal object, you can use conditional statements to
    control access to your code based on the principal name as shown in the
    following code example:

    WindowsPrincipa l MyPrincipal = (WindowsPrincip al) Thread.CurrentP rincipal;
    if (MyPrincipal.Id entity.Name == "fred")
    // Permit access to some code.

    You can also programmaticall y check role membership by calling the IsInRole
    method on the current Principal object as shown in the following code
    example:

    WindowsPrincipa l MyPrincipal = (Thread.Current Principal as
    WindowsPrincipa l);
    if (MyPrincipal.Is InRole("Adminis trator")) {
    // Permit access to some code.
    }

    The examples are from the MSDN Library in dotnet security section, if you
    need detailed information on it, you can visit
    this topic directly via the following weblink:
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

    rincipalobject. asp?frame=true


    Please try out the above suggestion. If you have any questions, please feel
    free to let me know.


    Merry Christmas!!

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Comment

    Working...