6

I'm trying to implement authentication by use of JSON Web Tokens (JWT) in Unity 3D. I searched a lot in google and GitHub and found nothing useful. There is a .NET library in GitHub but i don't know how to use it.

https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet

I'm new to unity and any help with this would be great.

hojjat reyhane
  • 648
  • 1
  • 8
  • 25

3 Answers3

3

Here is one way to decode a JSON web token with C# in Unity

var parts = token.Split('.');
if (parts.Length > 2)
{
    var decode = parts[1];
    var padLength = 4 - decode.Length % 4;
    if (padLength < 4)
    {
        decode += new string('=', padLength);
    }
    var bytes = System.Convert.FromBase64String(decode);
    var userInfo = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • You should have at least stated that your code is a security vulnerability. You left the signature unverified. – ZARKONES Mar 09 '22 at 20:43
2

Sorry for delay,

I found this: Jwt.Net

It actually supports the proper .NET version you need for unity3d which is 3.5 Just try to find DLLs, then put them in your unity project.

Hope it helps.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Alireza Sanaee
  • 465
  • 1
  • 7
  • 21
0

It won't work with unity because that is for .Net 3.5. (https://github.com/jwt-dotnet/jwt)

Unity is still using .Net 2.0 @ version 5.2

YukiNyaa
  • 136
  • 1
  • 13