473,408 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Security.PrincipalPermission.Demand()

Hi,

Normally my programming style is as follows:

/****************/
if ( SomeObject.CanYouDoThis() == true)
SomeObject.DoStuff();
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
PrincipalPermission'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
Jan 14 '08 #1
3 3242
Bump?

-Jeroen
Jan 15 '08 #2
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 [PrincipalPermission(Demand...blah)] 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
Jan 15 '08 #3
Thanks Marc, that answers my question.

-Jeroen
Jan 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: craig | last post by:
I am working on my first .NET development project that involves custom role-based security per the project requirements. This lead to a general design issue this week that really caused us some...
1
by: localhost | last post by:
I have decorated several classes and methods in an ASP.NET appliation with declarative security attributes for roles. For example: I currently use a Try...Finally block in calling code to...
1
by: Jason | last post by:
Hi I have a ASP.NET application where i would like to authenticate the connecting users according to the Local Users and Groups on the web server. I have the following code in the ASP.NET...
3
by: xenophon | last post by:
I am using Role-based seucity Attributes on different methods in my code-behind like this: private void callmymethod() { try { mymethod(); } catch{}
3
by: Dave Wurtz | last post by:
All, Does anyone have ideas how they have implemented field (property) level security? I want to handle this from the business object level, not the database level. Is it best to have a...
1
by: Paul | last post by:
I was working with a VB book that was going over the Security stuff in VBNet, and I can't seem to get their code to work. I modified it and I can't seem to get it to work either. I am running...
1
by: Ray Cassick \(Home\) | last post by:
I am having a bit of a problem doing something that I was expecting to be very straight forward. I am trying to declare a Role that needs to be available before a specific class can be...
3
by: Ray Cassick \(Home\) | last post by:
I have some code that looks like this for one of my classes: Imports System.Security Imports System.Security.Permissions Imports System.Security.Principal ...
0
by: wk6pack | last post by:
Hi, I'm trying to get my application to authenticate using role based when the user runs the application. When the user logs on and is in the security group "school", the user gets into the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.