Skip to main content

Method Authorization

Method Authorization allows you to have fine-grain control over how different types of API requests can be executed.
Since most of RESTier uses built-in convention over repetitive boiler-plate Controllers, you can’t just add security attributes to the controller methods, like you can with Web API.
However, there are two different methods for defining per-request security. One, like the rest of RESTier, is convention-based, and the other executes before every request, allowing you to centralize your authorization logic. This allows you to pick the approach that works best for your architecture.
No matter what approach you choose, the concept is simple. Either technique uses a function that returns boolean:
  • Return true, and processing continues normally
  • Return false, and RESTier returns a 403 Unauthorized to the client

Convention-Based Authorization

Users can control if one of the four submit operations is allowed on some EntitySet or Action by putting some protected internal methods into the Api class. The method name must conform to the convention:
The possible values for {Operation} are:
  • Insert
  • Update
  • Delete
  • Execute

Example

The example below demonstrates how both types of {TargetName} can be used:
Shows a simple way to prevent any user from deleting a particular EntitySet
Shows how you can integrate role-based security using multiple techniques
Shows how to prevent execution of a custom Action
TrippinApi.cs

Centralized Authorization

In addition to the more granular convention-based approach, you can also centralize processing into one location.
Users can use interface IChangeSetItemAuthorizer to define any customized authorize logic to see whether a user is authorized for the specified submit. If this method returns false, then the related query will get error code 403 (Forbidden).
There are two steps to plug in the centralized authorization logic:
1

Create authorizer class

Create a class that implements IChangeSetItemAuthorizer
2

Register with DI

Register that class with RESTier through Dependency Injection (DI)

Example

CustomAuthorizer.cs

Leveraging Both Techniques

There may be certain situations where you want to have a global interceptor, and then pass requests off to the individual convention-based interceptors. For example, if you need to authenticate a Bearer token. The example below shows you exactly how this type of scenario would work.

Example

Unit Testing Considerations

Because both of these methods are de-coupled from the code that interacts with the database, the Authorization logic is easily testable, without having to fire up the entire Web API + RESTier pipeline.

Setting up your Unit Test

If you don’t have a unit test project for your API project already, start by creating one. Repeat the process outlined in “Getting Started” to install the RESTier packages into your Unit Test project. The add the FluentAssertions package. Next, go back to your API project. Expand the “Properties” node, double-click AssemblyInfo.cs, and add the following line to the very end of the file: [assembly: InternalsVisibleTo("{TestProjectAssembly}")], making sure you replace with the actual assembly name. This is important, because otherwise the tests won’t be able to see the protected internal methods the authorization conventions use.

Example

Given the Convention-Based Authorization example, the tests below should have 100% code coverage, and should pass without any required changes.