Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
Xelit3 edited this page Jun 8, 2020 · 15 revisions

Description of the tool

ChustaSoft.Authorization is a tool based on Microsoft ASPNETCore Identity thought to have an easy and clear implementation and configuration of the tool inside a project with JWT authentication.

ChustaSoft.Authorization is also prepared to manage extended contexts, so if the target project requieres additional properties or relations it will also be able to handle it, otherwise, you also have the possibility of just use the default implementation inside.

With the version 2.0, the tool is now compatible with .NET Core 3.1, and is divided in two different packages:

  • ChustaSoft.Tools.Authorization: Main package, it contains the services, models and database context using Microsoft AspNet Identity features, isolating and extending in different services.
  • ChustaSoft.Tools.Authorization.AspNet: Package for adding controllers to the target project exposing main functionalities like Login or Register in a REST Controller.

Configuring and using

There are two different ways to use it, the default one, and the custom:

Default implementation

If the project doesn't requiere any additional functionality or information stored in the context provided by the Authorization tool, it is possible to use the default implementation provided internally by the tool. As a level services, the tool can provide of the different services. All this functionalities are using internal base User and Role:

  • ISessionService
    • Task AuthenticateAsync(Credentials credentials)
    • Task RegisterAsync(Credentials credentials)
  • IUserService
    • Task GetAsync(Guid userId)
    • Task GetByUsername(string username, string password)
    • Task GetByEmail(string email, string password)
    • Task CreateAsync(User user, string password)
  • IRoleService
    • Task Get(Guid roleId)

Steps for configuring the tool

  1. Add configuration section on appsettings.json:

"AuthorizationSettings": { "SiteName": "AuthorizationApi", "DefaultCulture": "en-UK", "MinutesToExpire": 60, "MinPasswordLength": 6, "StrongSecurityPassword": true },

  1. On Startup, in ConfigureServices:

services.RegisterAuthorization(_configuration, connectionString);

services.AddMvc().IntegrateChustaSoftAuthorization();

  1. Inject AuthorizationContext on Configure method

  2. On Configure method again, add this line:

app.ConfigureAuthorization(env, authContext);

This will update automatically migrations and other required staff

Custom extended implementation

  1. Add configuration section on appsettings.json:

"AuthorizationSettings": { "SiteName": "AuthorizationApi", "DefaultCulture": "en-UK", "MinutesToExpire": 60, "MinPasswordLength": 6, "StrongSecurityPassword": true },

  1. On Startup, in ConfigureServices:

services.RegisterAuthorization<AuthCustomContext, CustomUser, CustomRole>(_configuration, connectionString);

services.AddMvc().IntegrateChustaSoftAuthorization();

  1. Inject AuthCustomContext on Configure method

  2. On Configure method again, add this line:

app.ConfigureAuthorization<AuthCustomContext, CustomUser, CustomRole>(env, authContext);

This will update automatically migrations and other required staff

Services, additionally, in that case should be the base typed ones, ie: IUserService instead of IUserService With that option is also required to manage internally migrations, then it will also be applied automatically by the tool

Using without AspNet tool:

Base library has a extension method to configure in any case the tool, without configuring AspNet part:

  • RegisterAuthorizationCore<TAuthContext, TUser, TRole>(this IServiceCollection services, IConfiguration configuration, string connectionString)
    • If generic implementation is required, typed objects should be:
      • AuthorizationContext
      • User
      • Role
    • Otherwise, custom extensions could be defined and setup here

Functional use

AspNet project provides automatically of a Controller exposing the following actions:

  • Task Login([FromBody] Credentials credentials)
    • POST
    • [...]/api/auth/login
  • Task Register([FromBody] Credentials credentials)
    • POST
    • [...]/api/auth/login

That's all, enjoy! Whatever it may be required please do not hesitate to contact us

Thank you all :)

Clone this wiki locally