/WebApiOwinMiddleware

WebApi + Owin + Middlewares - e.g. OWIN Basic Authentication + REST Api Examples

Primary LanguageC#

Web API with Owin and middlewares

Exmaple web app with WebAPI, Owin and middlewares, e.g. OWIN Basic Authentication

Middlewares

Filtering Middlewares

  • HeaderFilterMiddleware - requires an configured HTTP header (e.g. X-my-sample-header) to be preset in every request configured in Startup -> app.UseHeaderFiltering(...).

    Configuration in web.config

    AppSettings["TokenHeaderName"]: the required header name
    AppSettings["TokenHeaderValue"]: the required header's value
    AppSettings["TokenHeaderFilteringEnabled"]: enable/disable header filtering. You can disable it for debug and enable for release.
    
  • IpFilterMiddleware - disabled, configured in Startup -> app.UseIpFiltering(...)

Authentication Middleware

  • BasicAuthenticationMiddleware - A resource that is protected by basic authentication - [Authorize] attribute - requires incoming requests to include the Authorization HTTP header using the basic scheme. This scheme uses a base64 encoded username and password separated by a colon (base64 encoding is used to avoid characters that would cause issues when sent over HTTP). OWIN Basic Authentication
Plain text
Authorization: Basic username:password

Encoded
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

Configuration in web.config

AppSettings["ApiUserName"]: user name for authentication
AppSettings["ApiPassword"]: password for authentication

REST Api

Example of using correct HTTP method in API:

  • GET - get single or all
  • POST - create or create lazy. Return Location header with URL to newly created object (status code: 201 Created) or where the object will be created when cannot be created immediatelly - the lazy option (status code: 202 Accepted) with Location header
  • PUT - update an object
  • PATCH - update only one property
  • HEAD - check if an object exists without returing data
  • DELETE - delete an object

Database

Database used is LiteDB - Embedded NoSQL database for .NET, stored in a single file. The database is initially setup in DatabaseSetup.cs

Resources: