How to decode JWT in PCL compatible with Xamarin Android

I have a Xamarin.Forms solution with PCL build, Android app and iOS app and I want to decode JWT in PCL.

I cannot 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 managed to add Jose JWT 1.7.0 from NuGet and make sure that it works in Unit Tests, but it makes me get the following build error in my Xamarin Android project ...

Assembly loading exception: System.IO.FileNotFoundException: Failed to load assembly 'System.Web.Extensions, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35'. Perhaps it does not exist in the Mono profile for Android?

Suggestions?

+6
source share
2 answers

As you can see from http://developer.xamarin.com/guides/android/under_the_hood/assemblies/ , the .NET Framework in Mono for Android does not seem to contain the System.Web.Extensions assembly

Edit: this means that you are linking to a library created for use on the desktop and not recompiled using the Android builds. This is not supported.

+6
source

I used the Portable.JWT library

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

This must be done in the Shared PCL project, not in 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; } } 
0
source

Source: https://habr.com/ru/post/1200877/


All Articles