Hello,
I'm trying to use C# as a script language for my .NET application.
Users able to write a method (say, Foo(Myclass parameter))
Then in run time I'm wrapping this method into namespace code, compile into
assembly signed with special key and execute.
Here is a problem - I'd like to apply some restrictions on user's code.
I.e. "Internet" set of permissions.
But it doesn't work. When I create code group for this special key, any
permission set besides "Full trust" gives me
"Security error". Even "Everything" set. I have "This policy level"
checkbox in Code Group properties dialog checked (if it not checked then
compiled assembly got all permissions from main application)
So, is it possible at all? Am I doing something wrong or may be just don't
understand something about security model?
Please, help!
Here is code snippet
CodeDomProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.GenerateInMemory = false;
compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.ReferencedAssemblies.Add(Path.Combine(Application.StartupPath
, "MyCompany.MyFramework.dll"));
string code = "[assembly: AssemblyKeyName(\"MyKey\")]";
code += myNamespaceAndMethodText;
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerParams, code);
object o = results.CompiledAssembly.CreateInstance("MyClass", true);
//this is where Security exception throws:
object retVal = o.GetType().InvokeMember("Foo", new object[]
{myClassInstance});
regards,
Ken
Henning Krause [MVP] - 26 Feb 2005 09:22 GMT
Hello,
I'm not very proficient on this topic, but I believe you should load the
newly created type into another app domain and execute it there.
The new app domain can be initialized with a custom set of evidence.
Greetings,
Henning Krause [MVP]
==========================
Visit my website: http://www.infinitec.de
Try my free Exchange Explorer: Mistaya
(http://www.infinitec.de/software/mistaya.aspx)
> Hello,
>
[quoted text clipped - 23 lines]
> compilerParams.GenerateInMemory = false;
> compilerParams.ReferencedAssemblies.Add("System.dll");
compilerParams.ReferencedAssemblies.Add(Path.Combine(Application.StartupPath
> , "MyCompany.MyFramework.dll"));
>
[quoted text clipped - 11 lines]
> regards,
> Ken