为不同用户共享具有不同访问权限的相同DLL

时间:2018-08-26 13:31:19

标签: c#

这个问题是在采访中提出的。我需要真正的澄清。这里的用户可以视为不同的客户端。

我的问题仅针对C#。我需要与三个用户共享同一DLL。问题是我需要给他们不同的访问权限。例如,假设我有一个实现S,D和M方法的类,但是我想要:

  1. 用户A可以访问所有三种S,D和M方法。
  2. 用户B只能访问D方法
  3. 用户C只能访问M方法。

虽然我有预感,可以使用接口来完成,但我无法理解核心实现。我正在寻找有关代码段的详尽而具体的解释。请记住,我们只需要与所有用户共享一个(相同)DLL。

2 个答案:

答案 0 :(得分:1)

您可以使用LicenseManager类与具有不同访问权限的其他用户共享同一dll

我将提供一个简单的POC解决方案

您为每个用户提供一个许可证密钥(因此您在单独的系统中有一个许可证生成器),用于控制已编译dll中ClientA,ClientB等的使用。

使用加密算法对licenseKey进行加密。

LicenceManager类可以是同一dll中的单调或静态类。

    //singletone  or static
    public class LicenceManager
    {
       // code for singletone
       //....
      private LicenceManager() { .... }

      public bool ClientA {get;set;}
      public bool ClientB {get;set;}
      public bool ClientC {get;set;}

      public void Activate (string licenceKey)
      {
        // decrept the key and and set ClientA,ClientB, ClientC based on the access rights
        // you should have KeyGenerator in a separate system that control the generation of the key
        // the key is delivered to user by email or internet.
        // options: licence key can be read from a file in disk or web site or licence server in LAN

      }

    }

用户调用方法:

        LicenceManager.Activate (string licenceKey)

此方法设置ClientA,ClientB,ClientC的Accces权限的属性

   //class  under access right protection
    public class Foo
    {
        //Control the access right for ClientA only
        public void S() 
        { 
          if (!LicenceManager.ClientA)
            throw new exception ("Not Valid key. Only ClientA licence can use this service");

            //do what actions
        }


        //Control ClientC
        public void D() 
        { 
           if (!LicenceManager.ClientC)
            throw new exception ("Not Valid key. Only ClientC licence can use this service");

            //do what actions
        }


       // and so on for other methods 
    }       

您应该考虑软件的黑客行为和反编译,并保护dll免受这些黑客攻击。

答案 1 :(得分:-1)

您是否肯定不是在同一程序集中为不同用户创建不同类的问题?我看不到有什么会阻止用户使用他们不该使用的类。

出于厚颜无耻,如果允许条件编译,则可以这样做:

public class Foo
{
    #if ClientA
    public void S() { }
    #endif

    #if !ClientC
    public void D() { }
    #endIf

    #if !ClientB
    public void M() { }
    #endif
}

有条件的项目文件包含也可以使用相同的方法:

public class FooBase
{
    protected void S() { }
    protected void D() { }
    protected void M() { }
} 

Foo_ClientA.cs

public class Foo : FooBase
{
    public new void S() { base.S(); }
    public new void D() { base.D(); }
    public new void M() { base.M(); }
}

Foo_ClientB.cs

public class Foo : FooBase
{
    public new void D() { base.D(); }
}

Foo_ClientC.cs

public class Foo : FooBase
{
    public new void M() { base.M(); }
}

DontDoThis.csproj

<Compile Include="Foo_$(Client).cs" />

这在实践中绝对是可怕的。同样,这并不能真正满足"in the same dll"的问题,因为尽管名称相同,但从技术上讲,您将创建功能上不同的dll。

相关问题