Unhandled Exception: System.Security.SecurityException

背景:我的应用程序可以运行用户指定的assembly中的代码,给个路径url就行了。

出事了: 用户报了一个bug,说当指定assembly的路径是本地路径,运行没问题,如果是非本地路径就会有一个错误提示。

Unhandled Exception: System.Security.SecurityException: Request for the permiss
on of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0
0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlM
rk& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessPermission.Demand()
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encodi
g, Int32 bufferSize)
   at System.IO.StreamWriter..ctor(String path)
   at COD_ITB.App_main_process.main()
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.FileIOPermission
The first permission that failed was:
<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Ver
ion=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

 

解决:当你运行一个非本地assembly时,需要对code access security进行必要的设置。

>caspol -m -ag 1 -url http://www.cnblogs.com/Kratos/admin/file:////z:\* FullTrust

it will grant fulltrust to z:\ assemblies.

你可能感兴趣的:(Unhandled Exception: System.Security.SecurityException)