0 Replies - 596 Views - Last Post: 12 September 2019 - 05:21 AM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

How to add payload when validate validate access token valid or not ?

Posted 12 September 2019 - 05:21 AM

problem
How to add payload when validate validate access token valid or not ?
how to validate access token have payload
i generate access token have payload user as following :

public string GenerateTokens(string userId, out object jwtcontent)
        {
            // jwtcontent = string.Empty;
            jwtcontent = new object();
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1"));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            var secToken = new JwtSecurityToken(
                signingCredentials: credentials,

                claims: new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, userId)
                });
               // expires: DateTime.UtcNow.AddDays(4));

            var handler = new JwtSecurityTokenHandler();
            var jsonu = new { id = userId };
            secToken.Payload["user"] = jsonu;
            return handler.WriteToken(secToken);
         
        }

when validate access token how to add payload users to access token that validated :

private static bool ValidateToken(string authToken)
        {
            try
            {
                var tokenHandler = new JwtSecurityTokenHandler();
                var validationParameters = GetValidationParameters();

                SecurityToken validatedToken;

                IPrincipal principal = tokenHandler.ValidateToken(authToken, validationParameters, out validatedToken);
                return true;
            }
            catch(Exception)
            {
                return false;
            }
            
        }
        private static TokenValidationParameters GetValidationParameters()
        {
            return new TokenValidationParameters()
            {
                ValidateLifetime = false, // Because there is no expiration in the generated token
                ValidateAudience = false, // Because there is no audiance in the generated token
                ValidateIssuer = false,   // Because there is no issuer in the generated token
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1"))// The same key as the one that generate the token
                
        };
        }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1