Skip to content

Test project to demonstrate how to use authentication in a Web API application and how to access it from a client.

Notifications You must be signed in to change notification settings

blasferatu/SecureAPITest

Repository files navigation

SecureAPITest

Test project to demonstrate how to use authentication in a Web API application and how to access it from a client.

Projects:

  1. SecureAPI - Web API application;
  2. Secure.API.Interfaces - Application interfaces;
  3. SecureAPI.Shared - Shared functionality between client and server;
  4. SecureAPIClient - Web API client.

Configuration

1. Web API (SecureAPI)

  • Controllers
    • 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.

  • Extensions

    ServiceExtensions - Contains the ConfigureJWT extension method that configures JSON Web Token usage.

  • Services

    AuthenticationService - Contains all authentication-related Web API functionality.

  • Startup.cs

    • 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();

2. Client application (SecureAPIClient)

  • Authentication

    AuthenticationClient - Contains all authentication-related Web API client functionality.

  • Program.cs

    Contains test code.

About

Test project to demonstrate how to use authentication in a Web API application and how to access it from a client.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages