我在c#中制作一个Android应用程序,它以登录页面开头。任何时候应用程序关闭,用户必须再次登录。
自动登录,在某处存储凭据并检索它们的最佳做法是什么? (如果这是最好的方式)
由于
的Riccardo
答案 0 :(得分:1)
您可以按照以下步骤操作
答案 1 :(得分:0)
在第一个你应该使用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();
}
}
}
}
答案 2 :(得分:0)
这个问题的答案在stackoverflow中(理解共享偏好是实现这一目标的最佳方法之后:
类的示例工作正常。
答案的最后一部分(作为最佳做法)是在检查令牌时,据我所知,每次应用程序联系服务器,检查令牌是否正确或已过期,或更改(由于密码更新)或用户删除)。
所以这个最佳实践应该是一个全局行为,例如在需要时更新令牌,并在应用与服务器交互时检查它。
感谢所有答案。