473,554 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Permission and SecurityManager .IsGranted

After reading Eugene Bobukh's blog entry about creating custom non-CAS
permissions, I developed a few custom permissions to satisfy the needs of an
application I'm currently working on.

For reference, the blog entry I'm referring to can be found here:

http://blogs.msdn.com/eugene_bobukh/.../10/87645.aspx

Everything Eugene talked about works fine, but one thing that doesn't seem
to work is using custom permissions with the SecurityManager .IsGranted
method. When SecurityManager .IsGranted is called, IPermission.IsS ubsetOf
gets called as well, but the IPermission target parameter is always null. I
verified that this happens with Euguene's sample code as well.

For example:

WorkingTimePerm ission p = new WorkingTimePerm ission();

if (SecurityManage r.IsGranted(p))
{
// Enable application UI tabs which should be available only during
working hours.
}

When stepping through the code, SecurityManager .IsGranted does some
processing and then WorkingTimePerm ission.IsSubset Of gets called, but with a
null IPermission target parameter.

Can anyone shed some light on why this may be happening?

Thanks in advance,

Jason
Nov 16 '05 #1
4 5456
"=?Utf-8?B?SmFzb24=?=" <Ja***@discussi ons.microsoft.c om> wrote in
news:8F******** *************** ***********@mic rosoft.com:
After reading Eugene Bobukh's blog entry about creating custom
non-CAS permissions, I developed a few custom permissions to
satisfy the needs of an application I'm currently working on.

For reference, the blog entry I'm referring to can be found
here:

http://blogs.msdn.com/eugene_bobukh/.../10/87645.aspx

Everything Eugene talked about works fine, but one thing that
doesn't seem to work is using custom permissions with the
SecurityManager .IsGranted method. When
SecurityManager .IsGranted is called, IPermission.IsS ubsetOf gets
called as well, but the IPermission target parameter is always
null. I verified that this happens with Euguene's sample code
as well.

For example:

WorkingTimePerm ission p = new WorkingTimePerm ission();

if (SecurityManage r.IsGranted(p))
{
// Enable application UI tabs which should be available
only during
working hours.
}

When stepping through the code, SecurityManager .IsGranted does
some processing and then WorkingTimePerm ission.IsSubset Of gets
called, but with a null IPermission target parameter.

Can anyone shed some light on why this may be happening?


Jason,

SecurityManager .IsGranted() determines whether a permission is
granted by examining the CAS permissions that have been granted by
the administrator. Since WorkingTimePerm ission is a non-CAS
permission, that means the security policies set by the administrator
have no impact regarding that permission. In other words, there is
no way for an administrator to grant or revoke a
WorkingTimePerm ission. Therefore SecurityManager .IsGranted() will
always return false for WorkingTimePerm ission().

WorkingTimePerm ission.Demand() is the method to use:
WorkingTimePerm ission p = new WorkingTimePerm ission();

try
{
p.Demand();
// If code gets here, then the permission was granted.

// Enable application UI tabs which should be available
// only during working hours.
}
catch (SecurityExcept ion ex)
{
// Permission was not granted.
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2
After digging through the rotor source, I was able to determine just that;
IsGranted won't work with non-CAS permissions. Having said that though,
what's the best method for applications to use when they want to know if a
demand will fail without having to catch SecurityExcepti on? I'm looking for
a simple method like IsGranted that returns bool. It looks exactly like
that's what IsGranted was tailored to do. Also, is there a reason IsGranted
isn't verifying that the IPermission provided is CAS related?

Thanks again,

Jason

"Chris R. Timmons" wrote:
"=?Utf-8?B?SmFzb24=?=" <Ja***@discussi ons.microsoft.c om> wrote in
news:8F******** *************** ***********@mic rosoft.com:
After reading Eugene Bobukh's blog entry about creating custom
non-CAS permissions, I developed a few custom permissions to
satisfy the needs of an application I'm currently working on.

For reference, the blog entry I'm referring to can be found
here:

http://blogs.msdn.com/eugene_bobukh/.../10/87645.aspx

Everything Eugene talked about works fine, but one thing that
doesn't seem to work is using custom permissions with the
SecurityManager .IsGranted method. When
SecurityManager .IsGranted is called, IPermission.IsS ubsetOf gets
called as well, but the IPermission target parameter is always
null. I verified that this happens with Euguene's sample code
as well.

For example:

WorkingTimePerm ission p = new WorkingTimePerm ission();

if (SecurityManage r.IsGranted(p))
{
// Enable application UI tabs which should be available
only during
working hours.
}

When stepping through the code, SecurityManager .IsGranted does
some processing and then WorkingTimePerm ission.IsSubset Of gets
called, but with a null IPermission target parameter.

Can anyone shed some light on why this may be happening?


Jason,

SecurityManager .IsGranted() determines whether a permission is
granted by examining the CAS permissions that have been granted by
the administrator. Since WorkingTimePerm ission is a non-CAS
permission, that means the security policies set by the administrator
have no impact regarding that permission. In other words, there is
no way for an administrator to grant or revoke a
WorkingTimePerm ission. Therefore SecurityManager .IsGranted() will
always return false for WorkingTimePerm ission().

WorkingTimePerm ission.Demand() is the method to use:
WorkingTimePerm ission p = new WorkingTimePerm ission();

try
{
p.Demand();
// If code gets here, then the permission was granted.

// Enable application UI tabs which should be available
// only during working hours.
}
catch (SecurityExcept ion ex)
{
// Permission was not granted.
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #3
"=?Utf-8?B?SmFzb24=?=" <Ja***@discussi ons.microsoft.c om> wrote in
news:C1******** *************** ***********@mic rosoft.com:
After digging through the rotor source, I was able to determine
just that; IsGranted won't work with non-CAS permissions.
Jason,

Are you familiar with Reflector? It's a great utility for
disassembling the .Net framework methods to find out what's really
being executed:

http://www.aisto.com/roeder/dotnet/
Having said that though, what's the best method for applications
to use when they want to know if a demand will fail without
having to catch SecurityExcepti on? I'm looking for a simple
method like IsGranted that returns bool. It looks exactly like
that's what IsGranted was tailored to do.
It took me a while to get used to CAS vs. non-CAS permissions, and
to realize that key phrases like "security policies" and "policy"
only apply to CAS permissions. Once I got comfortable with that,
deciphering apparently innocent help entries like
SecurityManager .IsGranted's Remarks section became much easier:

"Granting of permissions is determined by policy..."

This implies - but doesn't explicitly state - that the method only
works with CAS permissions, because it is checking the current
security policy. It takes some getting used to.

I don't think there's a method in the framework that could
take a WorkingTimePerm ission parameter and determine if
its permission had been granted. Non-CAS permissions are what
I would dub "stand alone" permissions. They are unique by
nature and have no required dependencies except for IPermission.
All CAS permissions, on the other hand, are tied to the security
policies set by the administrator.

However, there is nothing to prevent you from creating your
own SecurityManager-type class that handles both CAS and
non-CAS permissions:
using System.Security ;

// Untested.
public class MySecurityManag er
{
// Usage: MySecurityManag er.IsGranted(pe rmissionInstanc e);

public static bool IsGranted(objec t perm)
{
// perm descends from CodeAccessPermi ssion, so it's a
// CAS permission.
if (perm is CodeAccessPermi ssion)
return SecurityManager .IsGranted(perm as IPermission);

// perm does not descend from CodeAccessPermi ssion,
// but it implements the IPermission interface.
// That means it's a non-CAS permission.
if (perm is IPermission)
{
try
{
(perm as IPermission).De mand();
return true;
catch
{
return false;
}
}

// perm is not a permission.
return false;
}
}
Also, is there a
reason IsGranted isn't verifying that the IPermission provided
is CAS related?


Yes, there is. SecurityManager .IsGranted takes an IPermission
parameter. IPermission provides no way of determining the
parentage of the perm parameter. So IsGranted can't tell if the
permisson descended from CodeAccessPermi ssion or not.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #4
"Chris R. Timmons" wrote:
"=?Utf-8?B?SmFzb24=?=" <Ja***@discussi ons.microsoft.c om> wrote in
news:C1******** *************** ***********@mic rosoft.com:
After digging through the rotor source, I was able to determine
just that; IsGranted won't work with non-CAS permissions.
Jason,

Are you familiar with Reflector? It's a great utility for
disassembling the .Net framework methods to find out what's really
being executed:

http://www.aisto.com/roeder/dotnet/


Yep, I use Reflector all the time. I happened to be looking at some of the
Rotor source earlier so that that was a easier route at the time. But I
agree, Reflector is a godsend.
Having said that though, what's the best method for applications
to use when they want to know if a demand will fail without
having to catch SecurityExcepti on? I'm looking for a simple
method like IsGranted that returns bool. It looks exactly like
that's what IsGranted was tailored to do.
It took me a while to get used to CAS vs. non-CAS permissions, and
to realize that key phrases like "security policies" and "policy"
only apply to CAS permissions. Once I got comfortable with that,
deciphering apparently innocent help entries like
SecurityManager .IsGranted's Remarks section became much easier:

"Granting of permissions is determined by policy..."

This implies - but doesn't explicitly state - that the method only
works with CAS permissions, because it is checking the current
security policy. It takes some getting used to.


Thanks for the explaination. After spending a few days getting intimitely
familiar with the .NET security system, everything is making a lot more sense.

I don't think there's a method in the framework that could
take a WorkingTimePerm ission parameter and determine if
its permission had been granted. Non-CAS permissions are what
I would dub "stand alone" permissions. They are unique by
nature and have no required dependencies except for IPermission.
All CAS permissions, on the other hand, are tied to the security
policies set by the administrator.

However, there is nothing to prevent you from creating your
own SecurityManager-type class that handles both CAS and
non-CAS permissions:

I figured as much. I already have a SecurityManager like class in version 1
of the security library I developed for the last version of our product that
I'll easily integrate a similar method into. I am mainly concerned about
those developers who out of ignorance assume it's ok to pass my non-CAS
IPermission implementations into SecurityManager .IsGranted.

Thanks again for your help!

using System.Security ;

// Untested.
public class MySecurityManag er
{
// Usage: MySecurityManag er.IsGranted(pe rmissionInstanc e);

public static bool IsGranted(objec t perm)
{
// perm descends from CodeAccessPermi ssion, so it's a
// CAS permission.
if (perm is CodeAccessPermi ssion)
return SecurityManager .IsGranted(perm as IPermission);

// perm does not descend from CodeAccessPermi ssion,
// but it implements the IPermission interface.
// That means it's a non-CAS permission.
if (perm is IPermission)
{
try
{
(perm as IPermission).De mand();
return true;
catch
{
return false;
}
}

// perm is not a permission.
return false;
}
}
Also, is there a
reason IsGranted isn't verifying that the IPermission provided
is CAS related?


Yes, there is. SecurityManager .IsGranted takes an IPermission
parameter. IPermission provides no way of determining the
parentage of the perm parameter. So IsGranted can't tell if the
permisson descended from CodeAccessPermi ssion or not.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #5

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

Similar topics

3
1462
by: Nick | last post by:
My client uses a SQL Database to store their usernames and passwords, and I do not believe they have AD...no big deal... I wrote a class to create a generic identity and generic principal so that I can use the .IsInRole function for some added security. I would like to do the same by applying an attribute to a method or class. The code I am...
6
3054
by: deko | last post by:
Is there a way to set a custom property on Access tables and/or queries to prevent them from being overwritten by import wizards? Any Access database can be easily destroyed if a user mistakenly imports an object (or Excel spreadsheet) with the same name as an existing table or query. Import wizard: "Overwrite existing table/query?" ...
2
4240
by: Narshe | last post by:
I have a custom action for my msi installer where you select a directory, and some files get copied to that directory. I'm using System.IO.File.Copy() to copy them. If the files are already there, they are supposed to get overwritten, but I'm getting an access denied error. The files that are currently there have RA permissions. If i remove...
9
13888
by: Nick | last post by:
the customError feature is not working. I have it setup as the help says in my web.config file. <customErrors defaultRedirect="DsAppError.aspx" mode="RemoteOnly"/> I tried in a couple different parts of my site to throw a dummy exception and I always get to the page that says change my web.config to the statement above.
1
3334
by: Earl Teigrob | last post by:
Background: When I create a ASP.NET control (User or custom), it often requires security to be set for certain functionality with the control. For example, a news release user control that is comprised of a DataGrid may have separate permissions for adding, deleting and updating a news item. Problem Up until now, I have been implementing...
5
2516
by: Graham | last post by:
I have created a custom MembershipProvider called "LassieMembershipProvider" that derives from "MembershipProvider". This providor is located in a Businesslogic layer dll called "Enlighten.LinkMad.Businesslogic". In one of my frontend websites I use this type to authenticate a user who is trying to login. The following excerpt is from the...
5
6936
by: Jon Skeet [C# MVP] | last post by:
I've run against a problem which I'm *sure* must be easy to solve - but I'm blowed if I can find the answer :( I have a web service which I want to require authentication. I need to authenticate using a database lookup, so Windows, Passport and Forms authentication are (as far as I can tell) no good to me. I don't need impersonation. I...
0
2104
by: Atul Thombre | last post by:
Hello, I am developing a custom membership provider. For that I built a prototype that uses a SQL Server 2005 database as a backend store. I implemented the class System.Web.Security.MembershipProvider and implemented few necessary methods. The methods use SQL for interacting with the SQL Server database. I put all this code in a class...
5
7048
by: sayeo87 | last post by:
Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got: <%@ page import="java.io.*" %> <HTML> <BODY> <% Runtime rt = Runtime.getRuntime(); Process p = rt.exec("/bin/ls");
0
7600
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8042
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7889
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6145
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5155
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3560
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
841
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.