4
votes

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.

3
Hi, you cane create a HttpRequest and set it headrs for JWT authentication.Babak Fakhriloo
Can you explain it more? as i said i'm new to unity.hojjat reyhane
I dont know anything about unity, but I am sure in almost every framework, there are some api to connect to network. You can read this docs.unity3d.com/ScriptReference/Network.Connect.html doc. or this question stackoverflow.com/questions/8951489/unity-get-post-wrapperBabak Fakhriloo
I know how to connect to network and this is not my problem. I just want to Encode and Decode JSON with JWT in unity.hojjat reyhane
I saw this article that explains encode and decode. codeproject.com/Articles/890684/…Babak Fakhriloo

3 Answers

2
votes

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.

1
votes

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);
}
0
votes

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