Sunday 15 January 2012

Android Save Login and Storing of Login Information -



Android Save Login and Storing of Login Information -

today wanted implement login form. user has have connection net login because want send username , hashed password (sha 512?!) server , reply if username , password accepted or not.

the user should able store login optional. if user not store login has kind of session token can utilize total functionality of app until closes app. if user stores login should able automatically login app utilize total functionality @ time.

what 'best practices' next things: checking if user has connection net or not creating sha 512 hash password sending username , password server (save) getting save session token storing login info automatic login after restart

i read storing login in sharedpreferences dont know how can utilize create session token if user not want store login info , guess not save way.

ill seek reply of question aware not 'best practice' looking for. 1 possible way it.

1. checking if user has connection net or not

first need add together permissions manifest.xml:

<uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.internet" />

note first question need first of permissions. sec permission needed connect net when want send username , password.

now need connectivitymanager network informations. if network informations contain networkinfo.state.connected = true mobile device connected internet:

public static boolean isconnectedtointernet(context context) { connectivitymanager connectivity = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (connectivity != null) { networkinfo[] info = connectivity.getallnetworkinfo(); if (info != null) { (int = 0; < info.length; i++) { if (info[i].getstate() == networkinfo.state.connected) { homecoming true; } } } } homecoming false; }

i recommend check net connection first before seek connect net avoid exceptions.

2. creating sha 512 hash password

use standard messagedigest java security this. consider post:

private static final char[] hex_array = ("0123456789abcdef").tochararray(); public static string getsha512hahsofstring(string tohash) throws nosuchalgorithmexception, unsupportedencodingexception { string hash = null; messagedigest digest = messagedigest.getinstance("sha-512"); byte[] bytes = tohash.getbytes("utf-8"); digest.update(bytes, 0, bytes.length); bytes = digest.digest(); hash = bytestohex(bytes); homecoming hash; } public static string bytestohex(byte[] bytes) { char[] hexchars = new char[bytes.length * 2]; (int j = 0; j < bytes.length; j++) { int v = bytes[j] & 0xff; hexchars[j * 2] = hex_array[v >>> 4]; hexchars[j * 2 + 1] = hex_array[v & 0x0f]; } homecoming new string(hexchars); } 3. sending username , password server (save)

i utilize ssl purpose need valid certificate purpose. basic solution be:

defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); homecoming httpentity.getcontent();

where url url (secure(https)) webpage , params list of key-value pairs of parameters want post.

4. getting save session token 5. storing login info automatic login after restart

i set 4 , 5 because reply belongs both of them. can create sessiontokens simple using singleton pattern. when user logs in , not want save login info need create singleton session deleted automatically after app closed or user logs out:

public class sessionmanagersingleton { private context context = null; private static sessionmanagersingleton sessionmanagersingleton = null; private boolean isloggedin = false; private sessionmanagersingleton(context context) { this.context = context; this.isloggedin = false; } public void login() { this.isloggedin = true; } public void logout() { this.isloggedin = false; intent newintent = new intent(this.context, loginactivity.class); newintent.addflags(intent.flag_activity_clear_task); newintent.setflags(intent.flag_activity_new_task); this.context.startactivity(newintent); } public boolean isloggedin() { homecoming this.isloggedin; } public static sessionmanagersingleton getinstance(context context) { if(sessionmanagersingleton == null) { sessionmanagersingleton = new sessionmanagersingleton(context); } homecoming sessionmanagersingleton; } }

in activities user needs logged in need check sessionmanager using isloggedin().

if want store login info when user closes app can utilize shared preferences instead:

public class sessionmanagerpreferences { private context context = null; private sharedpreferences sharedpreferences = null; private editor editor = null; public sessionmanagerpreferences(context context){ this.context = context; this.sharedpreferences = this.context.getsharedpreferences(globals.key_pref_name, globals.private_mode); this.editor = this.sharedpreferences.edit(); } public void login(){ this.editor.putboolean(globals.key_logged_in, true); this.editor.commit(); } public void logout() { this.editor.clear(); this.editor.commit(); intent newintent = new intent(this.context, loginactivity.class); newintent.addflags(intent.flag_activity_clear_task); newintent.setflags(intent.flag_activity_new_task); this.context.startactivity(newintent); } public boolean isloggedin(){ homecoming this.sharedpreferences.getboolean(globals.key_logged_in, false); } } wont specify things deeper because big answer. if want more details please specify questions.

android

No comments:

Post a Comment