How do you check if you are running in Medium Trust environment in .NET?

為{幸葍}努か 提交于 2019-12-12 10:17:00

问题


I am running a website on shared hosting at GoDaddy (not my choice, because it is always Medium Trust), and I have some advanced features that I would like to turn on if the application is run in High Trust.

So would like to know, if there is an way to check if at runtime if the application is running in Medium Trust environment in .NET?


回答1:


You could try the following code:

if (!SecurityManager.IsGranted(new RegistryPermission(PermissionState.Unrestricted)))
{
    //do something.... not at full trust
}

if (!SecurityManager.IsGranted(new DnsPermission(PermissionState.Unrestricted)))
{
    //do something.... not at full trust
}

I got this from the following link: http://www.netomatix.com/development/webcaspermissions.aspx

here's links for the Security Manager and Registry Permission classes in MSDN:

http://msdn.microsoft.com/en-us/library/system.security.securitymanager.isgranted.aspx http://msdn.microsoft.com/en-us/library/system.security.permissions.registrypermission.aspx

You will need to add a reference to System.Security and add a couple of using statements for System.Security and System.Security.Permissions.

EDIT:

Added after Nick's Comment:

You could test directly for the asp.net security level:

if (SecurityManager.IsGranted( new AspNetHostingPermission(AspNetHostingPermissionLevel.Medium)))
{Response.Write("Medium Trust level");}


来源:https://stackoverflow.com/questions/1369059/how-do-you-check-if-you-are-running-in-medium-trust-environment-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!