Android Storing User Session in Shared Preferences

天涯浪子 提交于 2020-01-02 12:25:11

问题


I want to create a User Session on Android so that i do not have to login every time. What content should be stored in Shared Preferences so that i can authenticate every time my server gets a request from the user i can make sure people are not hacking into my system.

The users can login via the following in my app

  1. Facebook
  2. Google

Do i need to convert and store some encrypted data in Shared Preferences ?

Or just Storing the users Email or Username should be enough.


回答1:


Its easy to store the credential in shared preferences So that when you splash screen comes it you can check it and redirect the user to the next screen without asking user to Login into google or facebook.

I have used the preferences to login using facebook and our own server. For that i hae stored one boolean variable that user is is login with facebook or our own server then if the user loged in with our own server then we have called the webservice in background with stored usercreadential in preferences and if user loged in with facebook then we have usered

if (Application.prefs.isFacebookLogin()) {
        facebook = new Facebook(Application.APP_ID);
        // Instantiate the asynrunner object for asynchronous api calls.
        SessionStore.restore(facebook);
        SessionEvents.addAuthListener(new FbAPIsAuthListener());
        if (facebook.isSessionValid()) {
            Application.prefs.setAccessTokenFb(facebook
                    .getAccessToken());
            Application.prefs.setExpirationFB(facebook
                    .getAccessExpires());
        }
        // redirectHome();
        // finish();
    }

Here after that we have redirect user to the first screen if the creadential goes right.



来源:https://stackoverflow.com/questions/16622375/android-storing-user-session-in-shared-preferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!