Hardcoding user name and password in android app - android

I am creating an App and i want to store username and password in app itself. The username password is not entered by user. The credentials is common for everyone. I want to save the username and password in app code itself. What is the secure method to save in android?

You can use SharedPreference to store data permanently in the Android app.
In your Activity,
//get the sharedPrefs
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
//store the data in sharedPrefs file
with (sharedPref.edit()) {
putString("Some_key_user_name",userNameText)
commit()
}
//read the stored data form sharedPrefs
val storedUserName = sharedPref.getString("Some_key_user_name", defaultValue)
For the password, you can use the same approach. However, if the password is so precious(which it is in most cases) you can encrypt the data stored in sharedPreference file. Look ahead with this
As an example in my Activity,
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
with (sharedPref.edit()) {
putString("MY_HELLO_WORLD_KEY","helloWorld")
commit()
}

You can use key store to save your private keys securely
check this
Securely Storing Keys in Android Keystore
you can also use EncryptedPreferences to store simple data in an encrypted way.
check How to save secret key securely in android

Related

How to save password in sharedpreference?

I have done login and registration activity using rest API. But in login I used only Mobile number editext. And in registration I used password. So I want to use that password in login. Means user should enter that password while he used during registraton.
So how to do that?
If you're going to store passwords, make sure you store them in EncryptedSharedPreferences. You do not have to encrypt them manually as the library will take care of it.
If you need/want more explanation on this and how it works, check out some articles about it on the web - there are plenty.
If your using Kotlin, saving password in Shared Preferences could look like this:
Initialize Shared Preferences:
var prefs: SharedPreferences
prefs = getSharedPreferences("name_of_your_file", Context.MODE_PRIVATE)
Save password to it:
with (prefs.edit()) {
putString("password", etPasword.text)
apply()
}
If you want to access this stored password, you can do it like this:
val password = prefs.getString("password", "default_value")

How can I create a user-specific SharedPreference?

I'm trying to create a SharedPreference that is user specific. All I want to store is the layouts background depending on what is from the settings menu. How can I store and load SharedPreference depending on the Account being used?
You can use your user id as a key for SharedPreference and then save your user details as object in it's value.
In this way you store User data for all users locally.
/*Save the object in shared Preferences*/
public void saveObjectToPreferences(String key, Object value) {
final SharedPreferences prefs = getSharedPreferences(ApplicationConstants.PREF_FILE_CONFIG, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
String jsonValue = new Gson().toJson(value);
editor.putString(key, jsonValue);
editor.apply();
}
/*Get the object from shared Preferences*/
public Object getObjectFromPreferences(String key, Object defaultObj) {
final SharedPreferences prefs = getSharedPreferences(ApplicationConstants.PREF_FILE_CONFIG, Context.MODE_PRIVATE);
String value = prefs.getString(key, "");
Object object = new Gson().fromJson(value, defaultObj.getClass());
return object;
}
Pass user id in key and details in object in these methods. it will work.
Could you add a value field that contains the users information? Then you would search for the username and check it's children for corresponding information. You might have to store information as objects then.
If you are using Shared Preferences, it should only contain a small amount of key-value pairs. Since you are using a local database, I suggest storing your values there instead. You could have a table for each type of information you want to store, say High Scores. You would need a "USERID" field so that when you perform a search on the Database you could use the where clause to specify which USERID to search for. Once you have searched for the user's information you can store into Shared Preferences to make it easier to work with and only have a small amount of data as opposed to everyone's data. Once you're done with using the information you would save the information back to the DB.

Login with File Input/ android

I am a beginner in android development and i couldn't find out which logic should i apply.
I want to perform a login with File Input/Output in java,
Now i am able to write text to file, text is "username is admin password is admin"
Now when i want to read from this file, i want to apply some login that if the username written in file is "admin" and password written in file is "admin"
then show toast, else error.
I am not able to figure out that how to read keyword "admin" from this text file.
Thanks
Try this,
Create object of sharedPreferences
sharedPreferences = getSharedPreferences("loginDetails", Context.MODE_PRIVATE);
Store login details
private void setLoginDetails(String userName, String password) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userName",userName);
editor.putString("password",password);
editor.commit();
}
To check login
private boolean isValidUser(String userName, String password) {
if(sharedPreferences.getString("userName",null).equals(userName) && sharedPreferences.getString("password",null).equals(password))
return true;
else
return false;
}
**
Don't save password in sharedPreferences.
** If you really want Its best practice to Encrypt data and save in sharedPreferences. To validate get data from sharedPreferences and Decrypt it.
you can use android shared preference or database if you save a file in android sdcard any one can see this file and can modify it
I think you have not done your research. I have just TYPED "how to read from a file in android" in Google and got the answer in FIRST link. You must do research before asking question here.
By using StringBuilder and BufferedReader you can get all TEXT written in file. Store it in String type variable.
Than, You can EASILY find start, last index of searching word from that String type variable by using indexOf(searching word) and lastIndexOf(searching word).
You can get reference of that methods HERE.
By the way below link has your answer. Just check it out.
Read from text file
the only text that was stored was "username is admin password is admin" So i used string.split method and splited the string and stored it into array and then in the textview i saved the value at specified index of that array

Retrieving SharedPreferences

I'm working with an android application, and when my application is open I want to log in. I'm using shared preferences for saving data.
How can I control three cases: if his credentials are null, if that user exists and does not have to register and save his credentials again and also if username and password he entered is correct and is the same with them he used when entered for the first time.
Use this code to retrieve values from sharedPreferences.
SharedPreferences sharedPref ;
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int status=sharedPref.getInt("status",0);
if(status==1){
//do something here you want to do
}else if(status==2){
//do something here you want to do
}
Read the documentation here http://developer.android.com/reference/android/content/SharedPreferences.html
Also check this http://www.tutorialspoint.com/android/android_shared_preferences.htm

How to have android app remember user data/profile?

Making a app that asks user to create a profile, wondering where I should get started in having the app remember this data user inputs? Any tutorials or suggestions would be appreciated.
Thanks,
Grant
I suggest you use SharedPreferences, unless you have a lot of information to store.
After the user successfully logged in, store his information in your Preferences.
For example, to store the username :
private SharedPreferences mPreferences;
mPreferences = getSharedPreferences("User", MODE_PRIVATE);
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString("username", your_user_name);
editor.commit();
Each time the user accesses the login activity, you can check if the username is already stored in the preferences :
if (mPreferences.contains("username")) {
// start Main activity
} else {
// ask him to enter his credentials
}
When the user logs out, don't forget to delete the username key from your preferences :
SharedPreferences.Editor editor = mPreferences.edit();
editor.clear(); // This will delete all your preferences, check how to delete just one
editor.commit();
Insert the data to a SQLite database or use a plain file. The former is recommended for big apps.
Checked out SharedPreferences. Some examples here: SharedPreferences Tutorial
If you only have one user at once and just need to store simple user data like user name, email, id, you can store string/int/... format data in it. Or if you have server for storing user data, you can store credentials in SharedPreference and use the credentials to get data from your server.

Categories

Resources