C# JWT token invalid

I know it was driving me crazy for a few days.

And I believe their documentation needs a little update with a better explanation on the process.

Is there any way you could share your code with me? Iā€™ve tried updating the NodeJS code that Zoom provides, and it still doesnā€™t work, even if I add the ā€œiatā€ and ā€œaudā€ fields to the JWT payload. :frowning:

const payload = {
    iss: config.APIKey,
    exp: (new Date()).getTime() + 60000,
    iat: (new Date()).getTime() + 0,
    aud: null,
};
const token = jwt.sign(payload, config.APISecret);

Thanks again! You rock!

private
static
string GetToken()

{

string Key =
ā€œInsert Yoursā€;

string Secret =
ā€œInsert Yoursā€;

var zone = TimeZoneInfo.FindSystemTimeZoneById( ā€œPacific
Standard Timeā€);

var utcNow = DateTime.UtcNow;

var pacificNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, zone);

string s =
null;

        TimeSpan t = pacificNow.AddMinutes(Convert.ToInt32(5)) -

new DateTime(1970, 1, 1);

int secondsSinceEpoch = (int)t.TotalSeconds;

        secondsSinceEpoch = 1607803200;

// this value needs to be set from ZOOM web app, itā€™s when the token expires

        TimeSpan secpacific = TimeZoneInfo.ConvertTimeFromUtc(utcNow, zone) -

new DateTime(1970, 1, 1);

int NowsecondsSinceEpoch = (int)secpacific.TotalSeconds;

var token =
new { aud = s, iss = Key, exp = secondsSinceEpoch, iat = NowsecondsSinceEpoch };

        IJwtAlgorithm algorithm =

new HMACSHA256Algorithm();

        IJsonSerializer serializer =

new JsonNetSerializer();

        IBase64UrlEncoder urlEncoder =

new JwtBase64UrlEncoder();

return
new JwtEncoder(algorithm, serializer, urlEncoder).Encode(token, Secret);

    }

Thanks for sharing your code. It doesnā€™t work for my app. I hope Zoom support can help me. We are a paying customer (like yourself) and honestly, we expect much better documentation with a JWT token tool that actually works. Thanks again.

Iā€™ve opened a specific topic for our problem here: JWT app returns "invalid token"

Hey @nvanderson,

Thanks for the feedback, we will make our docs more clear on how to generate the JWT Tokens.

Thanks,
Tommy

Hey @Fitpass,

It seems your JWT App is effected with a cacheing issue we are having. Please private message me your account # so I can reset it.

Thanks,
Tommy

Hi nvanderson,

Thanks for shareing your code. May I ask which dotnet package youā€™re using for HMACSHA256Algorithm? could you have package name and version which works for you.
We suffer long time for this JWT token generation issue.

Thanks a lot.
Alan

using JWT;

using JWT.Algorithms;

using JWT.Serializers;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using RestSharp;

using System;

using System.Collections.Generic;

using System.IO;

using System.Net;

using System.Text;

var zone = TimeZoneInfo.FindSystemTimeZoneById(ā€œPacific Standard Timeā€);

var utcNow = DateTime.UtcNow;

var pacificNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, zone);

string s = null;

TimeSpan t = pacificNow.AddMinutes(Convert.ToInt32(5)) - new DateTime(1970, 1, 1);

int secondsSinceEpoch = (int)t.TotalSeconds;

secondsSinceEpoch = 1607803200; // this value needs to be set from web app, after Dec 2020

TimeSpan secpacific = TimeZoneInfo.ConvertTimeFromUtc(utcNow, zone) - new DateTime(1970, 1, 1);

int NowsecondsSinceEpoch = (int)secpacific.TotalSeconds;

var token = new { aud = s, iss = Key, exp = secondsSinceEpoch, iat = NowsecondsSinceEpoch };

IJwtAlgorithm algorithm = new HMACSHA256Algorithm();

IJsonSerializer serializer = new JsonNetSerializer();

IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();

return new JwtEncoder(algorithm, serializer, urlEncoder).Encode(token, Secret);

1 Like

Thanks a lot, Iā€™ll try that out

In Visual Studio there is a suggestion pop-up. I just picked it from there to auto download and install. If you are using VS, are you getting suggestions? If not, I will look more in the code tomorrow. Good night.

Your code work like a charm. Thanks a lot.
By the way, I use JetBrains Rider, and JWT package version 6.1.4.
image

packages\JWT.5.2.3\lib\net472\JWT.dll

namespace JWT.Algorithms

{

//

// Summary:

// HMAC using SHA-256

public sealed class HMACSHA256Algorithm : IJwtAlgorithm

{

public HMACSHA256Algorithm();

//

public string Name { get; }

//

public bool IsAsymmetric { get; }

//

public byte Sign(byte key, byte bytesToSign);

}

}

Just wondering, is any Zoom doc mention about this
secondsSinceEpoch = 1607803200; // this value needs to be set from web app, after Dec 2020

As I tested with AddMinutes(), which still not work.
(My local timezone is PST)

but when I add 12 hours as below, it works fine. I guess this related to timezone setting.

TimeSpan t = pacificNow.AddHours(12) - new DateTime(1970, 1, 1);
int secondsSinceEpoch = (int)t.TotalSeconds;

I hope Zoom dev should provide standard example for handling case of different local timezone (convert to UTC)?

anyway, thanks for your sharing.

This value should be custom set by you manually on the JWT generation token page. Itā€™s just a value I selected.

it all took me only 1 week to test and figure itā€¦LOL

Worked out like a charm with the JWT nuget package.

With framework 4.6+ you can use DateTimeOffset feature and convert to unix seconds for iat and exp.

In my case I did not need any timezone conversions.

Glad this is working, @nvanderson @alan.chen & @smaddu,

Weā€™ll update our documentation here for more specifics on UTC in these tokens.