AnjozAuthentication 1.2.1

dotnet add package AnjozAuthentication --version 1.2.1
                    
NuGet\Install-Package AnjozAuthentication -Version 1.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AnjozAuthentication" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AnjozAuthentication" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="AnjozAuthentication" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AnjozAuthentication --version 1.2.1
                    
#r "nuget: AnjozAuthentication, 1.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AnjozAuthentication@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AnjozAuthentication&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=AnjozAuthentication&version=1.2.1
                    
Install as a Cake Tool

Pacote com implementações padrão para autenticação

  • Nesse pacote foi realizado a implementação de algumas funcionalidades básicas para especializar e facilitar a utilização do ClaimsIdentity/IIdentity.
    Assim possibilitando ao desenvolvedor barrar o acesso diretamente no endpoint através das permissões e obter informações do usuário da requisição.

    Informações relevantes:

  • As permissões que o usuário possui serão obtidas através do tipo de claim: "Permissoes", exemplo:
    new Claim(Permissoes, "Claim1")
    
    IAnjozPrincipal.Permissions irá conter todas as permissões de claims do tipo Permissoes
    
    Assim quando o usuário possuir as claims será possível adicionar o atributo no endpoint do controlador ou no controlador:
    
    [AnjozPermissionAuthorize("PERMISSAO_NECESSARIA")]
    
  • Anjoz principal interface:
    public interface IAnjozPrincipal : IIdentity
    {
        string Id { get; } (Id usuário)
        string Email { get; } (Email usuário)
        ICollection<string> Permissions { get; } (Todas permissões vinculadas ao usuário)
        IEnumerable<Claim> Claims { get; } ( Todas claims do usuário geradas pelo servidor)
        public bool HasPermission(params string[] valoresPermissoes); (Método para verificação se usuário possui determinadas claims)
    }
    
  • Como utilizar:

    • Instalar o pacote e realizar a injeção da authenticação chamando o método:
      "AddAnjozAuthentication"
      Exemplo:
        IServiceProvider IStartup.ConfigureServices(IServiceCollection services)
        {
      
            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddFluentValidation(config => config.RegisterValidatorsFromAssemblyContaining(typeof(BootstrapDomain)));
      
            services.AddAnjozAuthentication()
      
            return services.BuildServiceProvider();
        }
      
      Para Utilizar a autenticação é necessário chamar o método:
      "UseAuthetication"
      Exemplo:
        public void Configure(IApplicationBuilder app)
        {               
            app.ConfigurarSwagger();
            app.UseHttpsRedirection();
            app.UseAuthetication();               
            app.UseMvc();
        }
      

      Para Registrar a autenticação obrigatória para todos os endpoints da api utilize o método:

      "RegisterFilterAnjozAuthorizantionGlobal"
      Exemplo:
            IServiceProvider IStartup.ConfigureServices(IServiceCollection services)
            {                   
                services.AddMvc(opt => opt.RegisterFilterAnjozAuthorizantionGlobal())                       
                services.AddAnjozAuthentication();                           
                return services.BuildServiceProvider();
            }
      
    • Utilizando "IAnjozPrincipal" em um serviço
      Após instalado o pacote e configurado, será possível utiliza a interface IAnjozPrincipal nos serviços se necessário, exemplo:
      public class ClaimService : CrudService<Claim, int>, IClaimService
      {
          private readonly IAnjozPrincipal _anjozPrincipal;
      
          public ClaimService(IClaimRepository claimRepository, IDomainServiceValidator<Claim> validador,
              IAnjozPrincipal anjozPrincipal) 
              : base(claimRepository, validador)
          {
              _anjozPrincipal = anjozPrincipal;
          }
      
          protected override Task<Claim> AoCriarAsync(Claim entity)
          {
              if (_anjozPrincipal.TemPermissao("PODE_GERAR_CLAIM"))
              {
                  entity.Valor = entity.Valor.ToUpperInvariant();
                  return base.AoCriarAsync(entity);
              }
      
              return default;
          }
      }   
      
    • Restringindo acesso a um endpoint pelas baseado nas permissões que o usuário possui, exemplo:
      public class CheckController : ControllerBase
      {
          [HttpGet]
          [AnjozPermissionAuthorize("PODE_ACESSAR_CHECK")]
          public IActionResult Check()
          {
              return Ok("ONLINE");
          }
      }
      

Obs:

Para o funcionamento deste pacote é necessário que a autenticação jwt ou outra forma de autenticação esteja configurada no projeto princípal.
De forma que o usuário da request esteja preenchido.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 1,065 3/12/2020
1.2.0 737 2/19/2020
1.1.1 1,131 1/9/2020 1.1.1 is deprecated because it is no longer maintained.
1.1.0 1,033 1/9/2020 1.1.0 is deprecated because it has critical bugs.
1.0.0 968 1/9/2020 1.0.0 is deprecated because it has critical bugs.