19 messaggi dal 13 maggio 2017
Buon giorno a tutti,
ho un problema con un comportamento diverso fra ambiente di sviluppo (mio computer) e ambiente di produzione (Hosting Windows su Aruba) di una procedura di autenticazione e autorizzazione utilizzando il Token JWT in ambiente .NET Framework.
Ho preso spunto da un sito (https://decatechlabs.com/secure-webapi-using-jwt) dove viene spiegato molto bene l'uso di Jwt Web Api c#.
Sul mio computer la procedura funziona perfettamente (provata con POSTMAN) ma quando vado a riportare la stessa procedura su Aruba, la procedura di Identificazione funziona bene (il Token viene generato correttamente), mentre nella fase di autorizzazione viene riportato un errore:
IDX10503: Signature validation failed. Keys tried: 'System.Text.StringBuilder'.
Exceptions caught:
'System.Text.StringBuilder'.
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.
La riga di codice che fa scattare il catch è questa:
//extract and assign the user of the jwt
Thread.CurrentPrincipal = handler.ValidateToken(token, validationParameters, out securityToken);
HttpContext.Current.User = handler.ValidateToken(token, validationParameters, out securityToken);
C'è qualcuno che può dirmi dove cercare, per trovare una soluzione?
Grazie
Vincenzo
2 messaggi dal 30 settembre 2020
Ciao, sto riscontrando lo stesso problema. Per caso hai risolto?
710 messaggi dal 13 novembre 2008
Contributi
ciao,
sembra un errore dovuto al tipo di Encoding, della chiave..?

prova così

avrai da qualche parte del codice tipo

string secret = "xxx";
var secKey = new SymmetricSecurityKey(secret);

prova a specificare un Encoding Base64

string secret = "xxx";
var key = Convert.FromBase64String(secret);
var secKey = new SymmetricSecurityKey(key);
2 messaggi dal 30 settembre 2020
Pultroppo no, ho gia provato.

Questo è il codice che utilizzo, potrebbe essere un problema di lunghezza della stringa _secret?

private const string _secret = "xxx";
        public static string CreateTokenForIdentity(Login identity)
        {
            byte[] key = Convert.FromBase64String(_secret);
            SymmetricSecurityKey securityKey = new SymmetricSecurityKey(key);
            SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[] {
                      new Claim(ClaimTypes.Email, identity.mail),
                      new Claim(ClaimTypes.Role, identity.admin.ToString())
                }),
                Expires = DateTime.UtcNow.AddMinutes(120),
                SigningCredentials = new SigningCredentials(securityKey,
                SecurityAlgorithms.HmacSha256Signature)
            };

            JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
            JwtSecurityToken token = handler.CreateJwtSecurityToken(descriptor);

            return handler.WriteToken(token);
        }

        public static IPrincipal getIdentity(string token)
        {
            
                JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
                JwtSecurityToken jwtToken = (JwtSecurityToken)tokenHandler.ReadToken(token);
                if (jwtToken == null)
                    return null;
                byte[] key = Convert.FromBase64String(_secret);
                TokenValidationParameters parameters = new TokenValidationParameters()
                {
                    RequireExpirationTime = true,
                    ValidateIssuer = false,
                    ValidateAudience = false,
                    ValidateIssuerSigningKey = false,
                    IssuerSigningKey = new SymmetricSecurityKey(key)
                };

                SecurityToken securityToken;
                ClaimsPrincipal principal = tokenHandler.ValidateToken(token, parameters, out securityToken);

                Thread.CurrentPrincipal = principal;

                return principal;
           
        }


Viene generata un'eccezione nella validazione.

    "Message": "An error has occurred.",
    "ExceptionMessage": "IDX10503: Signature validation failed. Keys tried: 'System.Text.StringBuilder'.\nExceptions caught:\n 'System.Text.StringBuilder'.\ntoken: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.",
    "ExceptionType": "Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException"


Grazie
Tonio

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.