コード例 #1
0
        protected virtual string Sign(string unsignedToken, JwtHeader header)
        {
            var algorithm = ParseSigningCredentials(header.SigningCredentials);

            if (header.SignatureAlgorithm != algorithm)
            {
                throw new InvalidOperationException("Mismatch between signature algorithm and signing key");
            }

            HMAC hmac;

            switch (algorithm)
            {
            case JwtConstants.SignatureAlgorithms.HMACSHA256:
                hmac = new HMACSHA256();
                break;

            case JwtConstants.SignatureAlgorithms.HMACSHA384:
                hmac = new HMACSHA384();
                break;

            case JwtConstants.SignatureAlgorithms.HMACSHA512:
                hmac = new HMACSHA512();
                break;

            default:
                throw new InvalidOperationException("Unsupported signature algorithm");
            }

            hmac.Key = (header.SigningCredentials.SigningKey as InMemorySymmetricSecurityKey).GetSymmetricKey();

            using (hmac)
            {
                var signature = hmac.ComputeHash(Encoding.UTF8.GetBytes(unsignedToken));
                return(Base64Url.Encode(signature));
            }
        }
コード例 #2
0
 public JsonWebToken()
 {
     Header = new JwtHeader();
     Claims = new Dictionary<string, string>();
     
     //Claims = new List<Claim>();
 }
コード例 #3
0
 public JsonWebToken()
 {
     Header = new JwtHeader();
     Claims = new List<Claim>();
 }
コード例 #4
0
        protected virtual string Sign(string unsignedToken, JwtHeader header)
        {
            var algorithm = ParseSigningCredentials(header.SigningCredentials);

            if (header.SignatureAlgorithm != algorithm)
            {
                throw new InvalidOperationException("Mismatch between signature algorithm and signing key");
            }

            HMAC hmac;

            switch (algorithm)
            {
                case JwtConstants.SignatureAlgorithms.HMACSHA256:
                    hmac = new HMACSHA256();
                    break;
                case JwtConstants.SignatureAlgorithms.HMACSHA384:
                    hmac = new HMACSHA384();
                    break;
                case JwtConstants.SignatureAlgorithms.HMACSHA512:
                    hmac = new HMACSHA512();
                    break;
                default:
                    throw new InvalidOperationException("Unsupported signature algorithm");
            }

            hmac.Key = (header.SigningCredentials.SigningKey as InMemorySymmetricSecurityKey).GetSymmetricKey();

            using (hmac)
            {
                var signature = hmac.ComputeHash(Encoding.UTF8.GetBytes(unsignedToken));
                return Base64Url.Encode(signature);
            }
        }