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

问题描述 投票:-1回答:2

在一次采访中询问了这个问题。我需要真正的澄清。这里的用户可以被视为不同的客户。

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

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

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

c#
2个回答
1
投票

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

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

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

许可密钥使用加密算法加密。

License Manager类可以是同一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)

此方法为ClientS,Client'S,ClientS设置访问权限的属性

   //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 
    }       

你应该考虑到软件和反编译的黑客攻击,你应该保护你免受这些黑客攻击。


-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尽管名称相同。

© www.soinside.com 2019 - 2024. All rights reserved.