Retrieve NamedPermissionSets at Runtime with C-Sharp

Sample C# code provided in order to retrieve needed informations about Security Policy settings in an hosting environment. Could be useful in custom trust configuration scenarios.

ISPs' hosting configuration settings can be very hostile in relation to web developers. A standard medium trust configuration can be customized so that it can be difficult to set a staging environment that perfectly match the online situation.

The variuos policy permission settings permit you to do some and deny to do some other.
They simply cause your application to work properly or crash because of security exceptions!

How to retrieve the information hidden in the {WINDIR}\Microsoft.NET\Framework\v2.0.50727\CONFIG folder?

  1. Kindly ask your provider to ...provide a sample of the .config file used in the farm.
  2. If you don't get answers because of unknow "security matters" (I just bring you my experience), embrace your C# loaded gun and start to ...code (nothing bloody)!

It takes just a couple of iteration through the right objects to get all the needed informations. Look at the following code...

// ...
System.Collections.IEnumerator levels =
    System.Security.SecurityManager.PolicyHierarchy();
while (levels.MoveNext())
{
    System.Security.Policy.PolicyLevel level = (System.Security.Policy.PolicyLevel)levels.Current;
    foreach (object nmed in level.NamedPermissionSets)
    {
        System.Security.NamedPermissionSet item = (System.Security.NamedPermissionSet)nmed;
        System.Collections.IEnumerator permissions = item.GetEnumerator();
        while (permissions.MoveNext())
        {
            System.Security.IPermission permission = (System.Security.IPermission)permissions.Current;
            System.Security.SecurityElement iperm = permission.ToXml();
            Response.Write("<xmp>" + iperm.ToString() + "</xmp>");
        }
    }
}

Hope it helped.

Take care. Bye.


Feedbacks

no feedbacks yet.

SL2