Catching Security Exceptions:

P

Praveen

Hi,

I have code like this in one of my type's constructor:

public MyConstructor()

{


try

{

AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(MyAssemblyResolver);

......

}

catch (System.Security.SecurityException){}

}

The idea is to catch the security exception that is thrown when this code is
run in partial-trust mode. But, this exception doesn't seem to get caught!
My asp.net application errors out with this message when run in
partial-trust mode:

[SecurityException: Request for the permission of type
'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
MyConstructor..ctor() +0

How do I catch this excpetion?

TIA,
Praveen
 
S

Steven Cheng[MSFT]

Hello Praveen,

From your description, you're going to use the "AppDomain.AssemblyResolve"
event to capture any potential .NET CAS SecurityException in your ASP.net
web application, but found it not work, correct?

As for the "AppDomain.AssemblyResolve" event, it is designed for capturing
the failure of loading a certain assembly in a certain .net AppDomain, for
example, if the runtime can not find a certain assembly required by the
appdomain(or version mismatch...), it will raise this event and call your
handler if you have registered. However, it does not care about any .NET
CAS specific exception(such as the SecurityException...)


For your scenaro, you're encountering some CAS specific SecurityException
in your ASP.NET application, I think this is because some assembly you used
or your asp.net application's components contains code that demand high
CAS permission which is not granted in partial trust environment. Such
exception would be throwed when a certain method(contains such code) be
executed. Generally, in ASP.NET, you can consider use "Application_Error"
event

#How to: Handle Application-Level Errors
http://msdn2.microsoft.com/en-us/library/24395wz3.aspx

===============
void Application_Error(object sender, EventArgs e)
{

Exception ex = HttpContext.Current.Server.GetLastError();

if (ex is System.Security.SecurityException)
{
Server.Transfer("~/errorinfo.aspx");
}
}
==================

In addition, for ASP.NET CAS issue, it is recommended that we check it at
development time before deployment, and if necessary, we can consider
customize the partial trust security policy or wrapper those privileged
assemblies before deployment. Here are some article describing how to deal
with CAS in asp.net 2.0(include partial trust scenario):

#How To: Use Medium Trust in ASP.NET 2.0
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html
/paght000020.asp

#How To: Use Code Access Security in ASP.NET 2.0
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html
/paght000017.asp

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top