Code: Detail

asp.net+core - (Portuguese)

Script de Geração e Validação de token

Script básico para gerar um token e valida-lo utiliznado JWT no Core

Last update: 06/07/2016
 
N/A
N/A
N/A
N/A
 

using Microsoft.IdentityModel.Tokens;

using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;


try
            {

   //  -------------- inicio codigo para gerar token ---------
                var tokenHandler = new JwtSecurityTokenHandler();

                var now = DateTime.UtcNow;

                var signingKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes("palavraChaveparageracaodoscript"));

                var _signingCredencials = new Microsoft.IdentityModel.Tokens.SigningCredentials(signingKey, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256);

                var tokenDescriptor = new JwtSecurityToken("emissor",
                                                            "publico",
                                                            new Claim[] {
                                                                new Claim(JwtRegisteredClaimNames.Sub, "Pedro"),
                                                                new Claim("Context", "Author")
                                                                    },
                                                            now,
                                                            now.Add(TimeSpan.FromMinutes(5)),
                                                            _signingCredencials
                                                            );


                var tokenString = tokenHandler.WriteToken(tokenDescriptor);



// ----------- fim codigo para gerar token -----------------

//  -------------- inicio codigo para validar token ---------




                var validationParameters = new TokenValidationParameters()
                {
                    IssuerSigningKey = signingKey,
                    ValidAudience = "audience",
                    ValidIssuer = "self"
                };

                SecurityToken _retSecurityToken;

                var principal = tokenHandler.ValidateToken(tokenString, validationParameters, out _retSecurityToken);

//  -------------- fim codigo para validar token ---------

            }
catch(Exception ex)
            {
                throw ex;

            }

Source:
 
Users who have marked this routine as a favorite
 
 
 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.