Identity.Token.Jwt.Management.ByNapster 1.1.0

dotnet add package Identity.Token.Jwt.Management.ByNapster --version 1.1.0
NuGet\Install-Package Identity.Token.Jwt.Management.ByNapster -Version 1.1.0
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="Identity.Token.Jwt.Management.ByNapster" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Identity.Token.Jwt.Management.ByNapster --version 1.1.0
#r "nuget: Identity.Token.Jwt.Management.ByNapster, 1.1.0"
#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.
// Install Identity.Token.Jwt.Management.ByNapster as a Cake Addin
#addin nuget:?package=Identity.Token.Jwt.Management.ByNapster&version=1.1.0

// Install Identity.Token.Jwt.Management.ByNapster as a Cake Tool
#tool nuget:?package=Identity.Token.Jwt.Management.ByNapster&version=1.1.0

Primeros Pasos

  1. Agregar al archivo appsettings.json el siguiente objeto json que tiene las configuraciones del proceso de Autorización por Token.
      "AppSettings": {
      "JwtSecretKey": "SecretKeyHere",
      "JwtAudienceToken": "https//jwt.io",
      "JwtIssuerToken": "https//jwt.io",
      "JwtExpireMinutes": "60" 
}
  1. Registrar las configuraciones en el Startup.cs
     // using Management.Identity.Tokens.Jwt.Models;

    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

  1. Generar Token en el servicio de autenticación

       // using Management.Identity.Tokens.Jwt.Helpers;
      //  using Management.Identity.Tokens.Jwt.Models;

      private readonly AppSettings _appSettings;

      public UserService(IOptions<AppSettings> options)
      {
          _appSettings = options.Value;
      }

      // Crear Claims
      // ClaimTypes.Name es requerido

      ClaimsIdentity claimsIdentity = new ClaimsIdentity(new[]{
            new Claim(ClaimTypes.Name,user.Username),
            new Claim("Rol",user.Rol),

        });

     var token = TokenGenerator.GenerateTokenJwt(claimsIdentity, _appSettings);

• Ejemplo


        public AuthenticateResponse Authenticate(AuthenticateRequest model)
        {
            var user = _users.SingleOrDefault(x => x.Username == model.Username && x.Password == model.Password);

            // return null if user not found
            if (user == null) return null;

            // authentication successful so generate jwt token

            //Create Claims

            ClaimsIdentity claimsIdentity = new ClaimsIdentity(new[]{
              new Claim(ClaimTypes.Name,user.Username),
              new Claim("Rol",user.Rol),

            });

            var token = TokenGenerator.GenerateTokenJwt(claimsIdentity, _appSettings);

            return new AuthenticateResponse(user, token);
        }

  1. Validar Token

• Agregar en el archivo Startup.cs (User es la clase/modelo que se usa para la autenticación de la app)


        app.UseMiddleware<JwtMiddleware<User>>();

• Heredar de la la interfaz IGenericTokenService y pasarle como argumento User


      // using Management.Identity.Tokens.Jwt.Interfaces;
      public class UserService : IUserService,IGenericTokenService<User>

• Implementar la interfaz y consultar el usuario por el valor del Claim registrado ClaimTypes.Name el la sección de crear claims


        public async Task<User> GetByName(string userName)
        {
            var user =  _users.SingleOrDefault(x => x.Username == userName);
            return user;
        }

  1. Agregar atributo a los diferentes controladores o clases que estarán protegidas.

       [Authorize]
       [ApiController]
       [Route("api/[controller]")]
       public class UserController : ControllerBase

       [Authorize]
       [HttpGet]
       public IActionResult GetAll()
       {
            var users = _userService.GetAll();
            return Ok(users);
       }

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. 
.NET Core netcoreapp3.1 is compatible. 
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.1.0 402 6/27/2022
1.0.0 403 6/27/2022