Test project to demonstrate how to use authentication in a Web API application and how to access it from a client.
- SecureAPI - Web API application;
- Secure.API.Interfaces - Application interfaces;
- SecureAPI.Shared - Shared functionality between client and server;
- SecureAPIClient - Web API client.
-
- AuthenticationController - Carries out authentication logic;
- DefaultController - This controller is invoked when an empty route is supplied;
- UsersController - Controller to exemplify the use of authorisation.
Add The Autorize attribute to the action methods or to the whole controller in order to protect the action methods:
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
See example in UsersController.cs.
-
ServiceExtensions - Contains the ConfigureJWT extension method that configures JSON Web Token usage.
-
AuthenticationService - Contains all authentication-related Web API functionality.
-
-
Add the following call in method ConfigureServices to configure JSON Web Token usage:
services.ConfigureJWT(Configuration); services.AddScoped<IAuthenticationService, AuthenticationService>();
-
Add the following code to the Configure method:
app.UseAuthentication(); app.UseAuthorization();
-
-
AuthenticationClient - Contains all authentication-related Web API client functionality.
-
Contains test code.