Code User Access Rights

We have a server written in C # (Framework 3.5 SP1). Clients write client applications using our server API. Recently, we have created several levels of licensing schemes, such as Basic, Intermediate and All. If you have a basic license, you can call several methods in our API. Similarly, if you have Intermediate, you get additional methods to call, and if you have All, you can call all methods.

When the server starts, it receives a license type. Now in each method, I have to check the type of license and decide whether to continue working with the function or return.

For example, the method InterMediateMethod()can only be used for the Intermediate License and All. So I need something like this.

public void InterMediateMethod()
{
   if(licenseType == "Basic")
    {
         throw new Exception("Access denied");
    }
}

It seems to me that this is a very lame approach. Is there a better way to do this? Is there any declarative way to do this by defining some custom attributes? I looked at creating a custom CodeAccessSecurityAttribute, but did not get a good success.

+3
source share
6 answers

Since you add “if” logic in each method (and God knows what else), it might be easier for you to use PostSharp (AOP framework) to achieve the same, but I personally don't like any of the approaches ...

, , ( ) , ( , ), , , .

, .

, ...

+2

, -API, API . , - , " ". (, , ) , . , ( ). , - "", . .

, API // "" , / " " (, TypeLoadException , ). , .

, , , "if", .

( ) :

  • , , .

  • PostSharp , .

  • PostSharp , (, XML , ). , .

, .

+2

, , . Desaware .

. , .

. "", "". , , , .

, - ASP.Net.

0

.
.
.

0

, SOA. .

0

@Ostati, 3 .

, 3 ( WCF) , . , - , .

0

Source: https://habr.com/ru/post/1739509/