c# - 将方法中的代码限制为仅调用同一类中的成员

标签 c# .net code-security

有没有一种方法可以限制在我的类中的一个方法中进行的调用,以便只能调用封闭类(也是继承的)上的方法和属性。我正在使用 c# (.NET 4.5)。这将在我们的应用程序框架中用作代码安全功能。

最佳答案

来自 How to: Run Partially Trusted Code in a Sandbox

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
using System.Reflection;
using System.Runtime.Remoting;

// The Sandboxer class needs to derive from MarshalByRefObject
// so that we can create it in another AppDomain and refer to
// it from the default AppDomain.
class Sandboxer : MarshalByRefObject
{
    const string pathToUntrusted = @"..\..\..\UntrustedCode\bin\Debug";
    const string untrustedAssembly = "UntrustedCode";
    const string untrustedClass = "UntrustedCode.UntrustedClass";
    const string entryPoint = "IsFibonacci";
    private static Object[] parameters = { 45 };

    static void Main()
    {
        // Setting the AppDomainSetup. It is very important to set the
        // ApplicationBase to a folder other than the one in which
        // the sandboxer resides.
        AppDomainSetup adSetup = new AppDomainSetup();
        adSetup.ApplicationBase = Path.GetFullPath(pathToUntrusted);

        // Setting the permissions for the AppDomain. We give the permission
        // to execute and to read/discover the location where the untrusted
        // code is loaded.
        PermissionSet permSet = new PermissionSet(PermissionState.None);
        permSet.AddPermission(
            new SecurityPermission(SecurityPermissionFlag.Execution));

        // We want the sandboxer assembly's strong name, so that we can add
        // it to the full trust list.
        StrongName fullTrustAssembly =
            typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>();

        // Now we have everything we need to create the AppDomain so,
        // let's create it.
        AppDomain newDomain = AppDomain.CreateDomain(
                "Sandbox",
                null,
                adSetup,
                permSet,
                fullTrustAssembly);

        // Use CreateInstanceFrom to load an instance of the Sandboxer class
        // into the new AppDomain. 
        ObjectHandle handle = Activator.CreateInstanceFrom(
            newDomain,
            typeof(Sandboxer).Assembly.ManifestModule.FullyQualifiedName,
            typeof(Sandboxer).FullName);

        // Unwrap the new domain instance into a reference in this domain and
        // use it to execute the untrusted code.
        Sandboxer newDomainInstance = (Sandboxer)handle.Unwrap();
        newDomainInstance.ExecuteUntrustedCode(
            untrustedAssembly,
            untrustedClass,
            entryPoint,
            parameters);
    }

    public void ExecuteUntrustedCode(
            string assemblyName,
            string typeName,
            string entryPoint,
            object[] parameters)
    {
        // Load the MethodInfo for a method in the new Assembly. This might be
        // a method you know, or you can use Assembly.EntryPoint to get to the
        // main function in an executable.
        MethodInfo target =
            Assembly.Load(assemblyName)
               .GetType(typeName)
               .GetMethod(entryPoint);

        try
        {
            //Now invoke the method.
            bool retVal = (bool)target.Invoke(null, parameters);
        }
        catch (Exception ex)
        {
            // When we print informations from a SecurityException extra
            // information can be printed if we are  calling it with a
            // full-trust stack.
            (new PermissionSet(PermissionState.Unrestricted)).Assert();
            Console.WriteLine(
                "SecurityException caught:\n{0}",
                ex);
            CodeAccessPermission.RevertAssert();
            Console.ReadLine();
        }
    }
}

关于c# - 将方法中的代码限制为仅调用同一类中的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935836/

相关文章:

c# - 在 Windows CE 的 Struct 中编码字符数组

c# - 在 Windows 窗体上绘制多个自定义矩形控件的最快方法是什么

c# - 从 ApplicationContext 退出应用程序

clang 没有停留在#include "/dev/whatever"

.net - System.Security.Permissions.DataProtectionPermissionAttribute 的使用

c# - 是否可以扩展 ServiceStack.ServiceInterface.Auth?

c# - 表达式。或者,参数 'item' 不在范围内

c# - 如何获取 Entity Framework 中的特定列

c# - 如何在我的 WP7 应用程序中序列化委托(delegate)

C# - 获取源代码文件正在使用或引用的类型列表