|
If I do a Process.Start() in a medium trust environment, I get a SecurityException.
This is not documented in VS .NET 2003.
What permission do Process & ProcessStartInfo require?
Frustrated in launching land. Mark Levison |
| MigrationUser 1 Thursday, April 10, 2003 5:12 PM |
Ok The rotor source code, tells me that Process Makes a LinkDemand for FullTrust.
From Process.cs: [ // Disabling partial trust scenarios PermissionSet(SecurityAction.LinkDemand, Name="FullTrust"), PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust") ]
How do I dynamically Assert or Demand FullTrust? Everything I can find on the subject talks about using the attributes. But I need a way of testing before we try to create a Process, do we have FullTrust?
|
| MigrationUser 1 Friday, April 11, 2003 10:27 AM |
Basically the way you can tell if you have a permission is to Demand() that permission. For example, to determine if you have unrestricted UIPermission, you would do the following:
(new UIPermission(PermissionState.Unrestricted)).Demand();
If this throws a SecurityException, then you do not have the permission. A simple utility method you could use might look like this:
// <summary> // Returns true if the calling code has been granted the specified permission, false // otherwise. // </summary> public static bool HavePermission(IPermission p) { try { p.Demand(); return true; } catch (SecurityException) { return false; } }
Hope that helps.
Kevin |
| MigrationUser 1 Wednesday, June 25, 2003 5:26 PM |
You can not dynamically get different levels of trust, you can only figure out whether or not you have the level of trust needed to execute what you want (in your case, FullTrust is required to run that line of code).
So you have to get FullTrust for your app ahead of time. Some people have done this through an install run on the client before running the app, etc...
When running an install locally, the install will have the same permissions the user would, hence why it can change the trust levels, but for now, you have to do this ahead of time. |
| MigrationUser 1 Monday, June 30, 2003 3:07 AM |
Eric what I was trying to was determine if my application has full trust or not. Not get it if we don't. Since my application will be launched over the internet, we've no control of the level of trust we will get. Asking the user to run an .msi to grant us full trust before we launch is just not option.
In the end I settled on this code fragement:public static bool IsFullyTrusted() { try { (new PermissionSet(PermissionState.Unrestricted)).Demand(); } catch (SecurityException) { return false; }
return true; }I'm not sure if its perfect - they maybe circumstances where it returns true, but we don't have full trust. However its the best I can do.
Thanks for the reply Mark |
| MigrationUser 1 Monday, June 30, 2003 11:44 AM |
Sorry about that, Mark! I guess I made too many assumptions from your post. That looks good! :) I'm wondering though if there's a way to do it without a try catch? I kind of doubt it, just curious, because using a try catch like that is considered bad practice and slows down your app a little, but if it's the only way, then that's definitely a good way! ;) |
| MigrationUser 1 Monday, June 30, 2003 1:12 PM |
Unfortuantely there is now way to test if you have a permission, with out demanding it and catching the resulting exception. However given the cost of the stack walk and everything else involved in checking the permission, the cost of catching a thrown exception is minimal. |
| MigrationUser 1 Monday, June 30, 2003 1:17 PM |
What about RequestMinimum?
[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]
I thought the idea was that you could request a minimum level of permissions and if you don't have it, your assembly won't load.
|
| MigrationUser 1 Wednesday, July 02, 2003 3:58 PM |
The problem is that you are using imperative security. If you use declarative security with RequestMinimum then the admin can see the minimum level of security that your assembly needs with the Permview utility. |
| MigrationUser 1 Wednesday, July 02, 2003 4:01 PM |
hi all,
this is an old post but I would like to add some comment because it puzzled me at first... I found out that you cannot make the security demand directly from your Main Method. When you do this, no exception will be thrown. You have to put the demand in a seperate class. Then it will work. See the code examples below: two tiny apps to check for FullTrust Permission. The first one does not work, the second one does.
================================================= EXAMPLE 1
// this doesn't work; no exception is thrown
using System; using System.Security; using System.Security.Permissions; using System.Windows.Forms;
public class MainForm : Form {
public MainForm() { }
public static void Main() {
try { SecurityPermission sp = new SecurityPermission( PermissionState.Unrestricted);
sp.Demand();
MessageBox.Show("Permission ok"); }
catch (SecurityException) {
MessageBox.Show("This App needs FullTrust Permission");
} }
}
================================================= EXAMPLE 2
// this works because the security demand is put in // a secondary class
using System; using System.Security; using System.Security.Permissions; using System.Windows.Forms;
public class MainForm : Form {
public MainForm() { }
public static void Main() {
try { //my own custom security check class: SecurityCheck sc = new SecurityCheck();
sc.DemandFullTrust();
MessageBox.Show("Permission ok"); }
catch (SecurityException) {
MessageBox.Show("This App needs FullTrust Permission");
} }
}
public class SecurityCheck { public SecurityCheck() { }
public void DemandFullTrust() { try { SecurityPermission sp = new SecurityPermission( PermissionState.Unrestricted); sp.Demand(); } catch (SecurityException) { throw new SecurityException(); }
} } ======================================================= |
| MigrationUser 1 Sunday, June 12, 2005 10:19 AM |