How to decode JWT in PCL that's compatible with Xamarin Android
Asked Answered
D

2

6

I have a Xamarin.Forms solution with a PCL assembly, Android application and iOS application, and I want to decode a JWT in the PCL.

I can't use Thinktecture.IdentityModel.Core 1.1.0 or System.IdentityModel.Tokens.Jwt 4.0.0 or JWT 1.3.2 because none of them can be added to a project that targets 'portable-net45+win+MonoAndroid10+MonoTouch10'.

I was able to add Jose JWT 1.7.0 from NuGet and verify that it works in Unit Tests, but it causes me to get the following build error in my Xamarin Android project...

Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Perhaps it doesn't exist in the Mono for Android profile?

Suggestions?

Dagney answered 21/8, 2014 at 19:11 Comment(0)
P
7

As you can see from http://developer.xamarin.com/guides/android/under_the_hood/assemblies/ the .NET framework in Mono for Android doesn't seem to contain the assembly System.Web.Extensions

Edit: this means that you are referencing a library that was created for desktop use, and has not been recompiled with the Android reference assemblies. This is not supported.

Productive answered 22/8, 2014 at 8:20 Comment(0)
T
0

I used a library called Portable.JWT

https://www.nuget.org/packages/Portable.JWT/

You need to do this is in a Shared PCL project though and not Xamarin.Android

You can do something like this:

public static class JwtDecoder
{
  public static long TokenExpirationTime(string token)
  {
    var decodedToken = JWT.JsonWebToken.DecodeToObject<Dictionary<string, object>>(token, default(byte[]), false);
    var exp = decodedToken["exp"];
    return (long)exp;
  }
}
Tiga answered 3/10, 2019 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.