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.
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 someprotected internal methods into the Api class. The method name must conform to the convention:
- Operation
- TargetName
The possible values for
{Operation} are:- Insert
- Update
- Delete
- Execute
Example
The example below demonstrates how both types of{TargetName} can be used:
Method 1: CanDeleteTrips
Method 1: CanDeleteTrips
Shows a simple way to prevent any user from deleting a particular EntitySet
Method 2: CanUpdateTrips
Method 2: CanUpdateTrips
Shows how you can integrate role-based security using multiple techniques
Method 3: CanExecuteResetDataSource
Method 3: CanExecuteResetDataSource
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).1
Create authorizer class
Create a class that implements
IChangeSetItemAuthorizer2
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.