Android c#App登录最佳实践

时间:2016-09-13 02:08:52

标签: c# android authentication xamarin login

我在c#中制作一个Android应用程序,它以登录页面开头。任何时候应用程序关闭,用户必须再次登录。

自动登录,在某处存储凭据并检索它们的最佳做法是什么? (如果这是最好的方式)

由于

的Riccardo

3 个答案:

答案 0 :(得分:1)

您可以按照以下步骤操作

  1. 成功登录后,将凭据保存在SQlite数据库或您正在使用的任何其他数据库中。
  2. 注销时,使用DeleteAll<>()方法清除这些凭据。
  3. 在启动/启动屏幕上,首先检查是否存在凭据。如果是,请向用户显示下一个屏幕,否则显示登录屏幕。
  4. 如果您不想删除凭据,请在User对象中使用布尔标记并相应地对其进行标记。
  5. 在密码更改期间,请务必使用新凭据替换旧凭据,否则用户可以使用他/她的旧凭据登录应用程序。

答案 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中(理解共享偏好是实现这一目标的最佳方法之后:

Here

类的示例工作正常。

答案的最后一部分(作为最佳做法)是在检查令牌时,据我所知,每次应用程序联系服务器,检查令牌是否正确或已过期,或更改(由于密码更新)或用户删除)。

所以这个最佳实践应该是一个全局行为,例如在需要时更新令牌,并在应用与服务器交互时检查它。

感谢所有答案。

相关问题