Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Security Attributes without Try-Finally?

Thread Tools

Security Attributes without Try-Finally?

Posts: n/a

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

(System.Security.Permissions.SecurityAction.Demand ,

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?

Reply With Quote
Steven Cheng[MSFT]
Posts: n/a
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 CurrentPrincipal
property on the System.Threading.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:

WindowsPrincipal MyPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal;
if (MyPrincipal.Identity.Name == "fred")
// Permit access to some code.

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

WindowsPrincipal MyPrincipal = (Thread.CurrentPrincipal as
if (MyPrincipal.IsInRole("Administrator")) {
// 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:

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!
(This posting is provided "AS IS", with no warranties, and confers no

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
data attributes override method attributes? Jayden Python 16 09-29-2012 03:20 AM
attributes ala java annotations or .Net attributes? Kyle Schmitt Ruby 3 07-24-2007 07:48 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM