是否可以限制或约束可以从 DLL 执行的 C# 代码的范围?
我正在开发一个项目,我将允许用户编写一些代码,这些代码将被编译、加载和运行,这些代码将允许用户控制应用程序的某些部分。然而,由于这只是允许他们将 C# 插入正在运行的应用程序中,所以我希望有一些限制。
有没有什么方法可以运行一段代码,但可以阻止任何 System.Threading
使用或任何 System.IO
使用?
我知道浏览代码并删除任何引用是可能的,但我确信会有办法解决这个问题。在写这篇文章之前,我至少想检查一下是否有更简单、更有效的方法。
我已经用谷歌搜索过,但要么我找错了地方,要么问了错误的问题。不过,我确实知道你们 Stack Overflow 的人都是天才。有什么想法吗?
谢谢,任何答案将不胜感激!
I am working on a project where I will allow users to write some code, which will be compiled, loaded and ran, this code will allow the users to control certain parts of the application. However, with this just allowing them to insert C# into the running app, there are certain restrictions I'd preferably like to have in place.
Are there any ways that you can run a piece of code but lets say block out any System.Threading
use or any System.IO
use?
I know going through the code and removing any references would be possible, but I am sure there would be ways around that. Before writing this, I'd at least like to check there isn't an easier, more effective way.
I have googled but either I am looking in the wrong place or asking the wrong question. I do know, however, that you Stack Overflow people are geniuses. Any ideas?
Thanks, and any answer would be appreciated greatly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码访问安全可能正是您所寻找的。
Code Access Security might be what you are looking for.
为什么不直接限制项目引用,而不是检查代码并删除所有单独的引用呢?此外,您还可以通过使用 .NET 框架配置管理单元来利用系统上的 .NET 权限,根据各种选项来管理对 GAC、域应用程序等的访问。 (开始 > 运行 > mscorcfg.msc 来启动它)但是我不知道更新的 4.0 选项是什么来替换该配置实用程序。编辑:要更进一步,您可以使用服务用户帐户运行输入的代码,并通过此实用程序限制该用户的代码访问。
Instead of going through the code and removing all the individual references, why not just limit the project references? Also, you might be able to leverage .NET permissions on a system by using the .NET framework configuration snap in to manage access to GAC, domain apps, etc. based on various options. (start > run > mscorcfg.msc to launch it) I don't know what the newer 4.0 option is to replace that configuration utility however. EDIT: To take it one step further you could, for example run the entered code with a service user account and limit that user's code access via this utility.