I'm making an android App in c#, and it starts with a login page. Anytime app get closed, user has to log in again.
What's the best practice to auto-login, storing credentials somewhere and retrieving them? (if this is the best way)
Thanks
Riccardo
You can follow this steps
in the first you should use ISharedPreferences
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using Android.Preferences;
namespace App
{
[Activity(Label = "App", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//to get data that already stored in Shared Preferences use prefs.get----
//to put new data in Shared Preferences Editor use editor.put----
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
if (prefs.GetBoolean("is_login",false))
{
//prefs.GetBoolean("key",defualt value)
//here use is logged in alread now open main page but note
//you need for exmaple his id to make some opration in main page after login
Intent x = new Intent(this,typeof(Activity2));
StartActivity(x);
}
else
{
//here user is not logged in now to can show login page and after that you need to put
//in local setting login=true like that
editor.PutBoolean("is_login", true);
editor.Apply();
}
}
}
}
Answer to this question is here in stackoverflow (after understanding sharedpreference is the best way to achieve this goal:
Example with class is working very fine.
Last part of the answer (as a best practice) is WHEN checking the token, and as far as I found, is every time app contact the server, checking if token is correct or expired, or changed (due to password update or user delete).
So this best practice should be a global behavior, for example updating token when needed, and checking it when app interacts with server.
Thanks for all answers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.