【问题标题】:where to save user session in mobile applications在移动应用程序中保存用户会话的位置
【发布时间】:2015-05-11 23:59:09
【问题描述】:

我有一个用于创建用户会话的 Web 服务,基本上它是一个登录 Web 服务。这个网络服务基本上创建了一个会话密钥,所以对于我需要访问的每个网络服务,我需要在我的标题中附加这个会话 ID,我的问题是在哪里存储这个会话 ID 的好地方,我基本上是新手在具有会话 ID 的移动开发中,不像 Web 应用程序,浏览器具有 cookie 和会话,但是在移动应用程序中呢?是否可以将其保存在 SQLite 数据库中?还是有其他方法来存储这个会话 ID?这样即使用户关闭应用并重新打开它,会话也会恢复。

顺便说一句,我正在使用 xamarin 创建我的移动应用程序,我实际上在考虑是否有一个存储可以用来在 iOS 和 android 中存储和恢复我的会话 ID。

谢谢

【问题讨论】:

标签: c# android ios xamarin


【解决方案1】:

如果您使用的是 android(不是 Xamarin),那么在 android 中使用 SharedPreferences 类并设置您的用户会话并获取用户会话。我编写的代码为。 首先创建名为 DataStore 的 SharedPreferences 类

package com.example.examplesharedpreferenced.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class DataStore {

    private static final String PREF_NAME = "com.example.examplesharedpreferenced.pref";
    public static final String KEY_SESSION = "key_session";

    public static void setUserSession(Context context, String session) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREF_NAME, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putString(KEY_SESSION, session);
        editor.commit();
    }

    public static String getUserSession(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREF_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(KEY_SESSION, null);
    }
}

然后在 Main Activity 中使用上述这些方法

package com.example.examplesharedpreferenced;

import com.example.examplesharedpreferenced.utils.DataStore;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //get user session
        String userSession = DataStore.getUserSession(MainActivity.this);
        Log.d(TAG, "userSession : "+userSession);

        //setting user session
        DataStore.setUserSession(MainActivity.this, "abc345asd");

        //get user session after setting it.
        String userSessionAfter = DataStore.getUserSession(MainActivity.this);
        Log.d(TAG, "userSessionAfter : "+userSessionAfter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

对于 Xamarin,您应该关注 link

【讨论】:

  • OP 说他正在使用 Xamarin.Android,它使用 C# 而不是 Java。
  • @ValOkafor 好的,我在 Xamarin stackoverflow.com/questions/26668509/… 中找到了 SharedPreferenced 的链接,感谢您更新我。
【解决方案2】:

我建议为您的应用创建一个私有 SharedPreferences,并在其中保存该值。但是我也建议让会话每隔一段时间过期一次,这样如果用户的手机被盗,他们将无法登录到您的应用程序并获取信息,假设已达到过期时间。

要保存到共享首选项:

// create a String for the SharedPreferences

private static final String PREFS = "MyAppsPrivatePrefs";
private static final String SESS_KEY = "Session";
private String session = "";

// then access preferences
SharedPreferences sharedPrefs = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
// Open preferences for editting
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(SESSION, session);
editor.commit();

这应该可以,但如果过了一段时间,我再次建议添加逻辑来清除它......

【讨论】:

    【解决方案3】:

    您可以使用 SharedPreferences。 http://developer.android.com/reference/android/content/SharedPreferences.html

    只有在用户清除缓存时才会删除首选项!

    【讨论】:

      猜你喜欢
      • 2020-02-24
      • 2021-12-23
      • 2014-04-28
      • 1970-01-01
      • 2014-09-18
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      相关资源
      最近更新 更多