0

I'm trying to access the current logged in user's data in Xamarin.Forms but there seems to be no answer online, I'm not trying to get the device's owner data, just the data about the user currently logged in to the app and using it such as its Id etc to query the user's Favorites and related data.

My app gets the user's identity from an ASP.NET CORE / .NET 5 API using Identity and JWT Tokens.

I know how to do it in ASP.NET CORE but I don't know how to access that information in Xamarin.Forms.

Thank you so much for your help !

Jimmy
  • 105
  • 15

1 Answers1

0

Assuming that you have a sign-in page, where the user enters his credentials that you send to your API and get a response, you can do two things.

  1. Return the user id along with the token and save it in the storage, e.g. by using the Preferences.
  2. Decode the JWT token and get the user id from there How to decode JWT Token
Almis
  • 3,684
  • 2
  • 28
  • 58
  • Thank you ! Which one do you think is the best way ? – Jimmy Oct 11 '21 at 21:05
  • Both ways are fine, you can even combine them, what I mean is decode token, save the user id in the Preferences, and then get the user Id from the Preferences to avoid getting the Token from the secure storage and then decoding it every time you need the user id. – Almis Oct 11 '21 at 21:10