2011-04-17 11 views
8

हे दोस्तों, मेरे पास Android में लॉगिन सुविधा लागू करने के बारे में कुछ प्रश्न हैं।एंड्रॉइड HTTP लॉगिन प्रश्न

1. Does android have anything like sessions or cookies? मुझे याद रखना चाहिए कि उपयोगकर्ता को लॉग इन किया गया है? जाहिर है, जब भी मेरा आवेदन इस्तेमाल होता है मैं पासवर्ड के लिए पूछना नहीं चाहता!

2. Should I hash the password before sending it to the server? मेरे पास उपयोगकर्ता और पासवर्ड कॉलम के साथ मेरे डेटाबेस में एक टेबल है। जब मैं लॉगिन की जांच करना चाहता हूं, तो क्या मुझे login.php?u=sled&p=34819d7beeabb9260a5c854bc85b3e44 जैसे सर्वर पर पासवर्ड डाला जाना चाहिए, या केवल login.php?u=sled&p=mypassword जैसे सादा पाठ भेजना चाहिए और प्रमाणीकरण करने से पहले सर्वर पर हैश?

उत्तर

10

क्या एंड्रॉइड में सत्र या कुकीज़ जैसी कोई चीज है?

हां। दो विकल्प हैं।

विकल्प # 1:

आप CookieManager का उपयोग अपने कुकी सेट कर सकते हैं।

विकल्प # 2:

अन्य वैकल्पिक (मैं अपने अनुप्रयोगों में से एक में इस विकल्प का उपयोग कर रहा) अपने कुकी हड़पने के लिए के बाद आप (सर्वर के लिए अपने यूज़रनेम और पासवर्ड भेज दिया है है जैसे के माध्यम से HttpPost या HttpGet)। आपके प्रश्न में आप अपने लॉगिन प्रमाणीकरण की $_GET शैली का उपयोग कर रहे हैं, इसलिए मेरा नमूना कोड HttpGet का उपयोग करेगा।

नमूना HttpGet का उपयोग कर कोड:

HttpParams httpParams = new BasicHttpParams(); 

// It's always good to set how long they should try to connect. In this 
// this example, five seconds. 
HttpConnectionParams.setConnectionTimeout(httpParams, 5000); 
HttpConnectionParams.setSoTimeout(httpParams, 5000); 

DefaultHttpClient postClient = new DefaultHttpClient(httpParams);   
// Your url using $_GET style. 
final String url = "www.yourwebsite.com/login.php?u=myusername&p=mypassword"; 
HttpGet httpGet = new HttpGet(url); 
HttpResponse response; 

try { 
    // Execute your HttpGet against the server and catch the response to our 
    // HttpResponse. 
    response = postClient.execute(httpGet); 

    // Check if everything went well. 
    if(response.getStatusLine().getStatusCode() == 200) { 
     // If so, grab the entity.   
     HttpEntity entity = response.getEntity(); 

     // If entity isn't null, grab the CookieStore from the response. 
     if (entity != null) { 
      CookieStore cookies = postClient.getCookieStore(); 
      // Do some more stuff, this should probably be a method where you're 
      // returning the CookieStore.  
     }     
    } 

} catch (Exception e) { 
} 

अब जब आप अपने CookieStore है; इससे कुकीज़ की एक सूची लें और इसके बाद आप नाम, डोमेन, मूल्य इत्यादि निर्धारित करने के लिए Cookie का उपयोग कर सकते हैं ...

अगली बार जब आप अपनी वेबसाइट की "लॉक" सामग्री तक पहुंचने का प्रयास कर रहे हैं; अपने Cookie जानकारी से अपने HttpURLConnection को एक कुकी सेट:

URL url = new URL("www.yourwebsite.com/lockedcontent.php"); 

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 

httpURLConnection.setInstanceFollowRedirects(false); 
// "Host" and "Cookie" are fields in the HTTP response. Use WireShark 
// via your computer to determine correct header names. 
httpURLConnection.setRequestProperty("Host", domainOfYourCookie); 
httpURLConnection.setRequestProperty("Cookie", valueOfYourCookie); 

final int responseCode = httpURLConnection.getResponseCode(); 

// And get the content... 

मैं इसे सर्वर को भेजने से पहले पासवर्ड हैश चाहिए?

इस पर निर्भर करता है कि आपका सिस्टम कैसे डिज़ाइन किया गया है। अपने सर्वर पर भेजते समय आपके पास सही जानकारी होनी चाहिए। यह इस बात पर भी निर्भर करता है कि आप अपनी .php फ़ाइल में अपनी जानकारी कैसे प्राप्त कर रहे हैं।

मुझे याद रखना चाहिए कि उपयोगकर्ता को लॉग इन किया गया है?

SharedPreferences या कुछ में जानकारी संग्रहीत करें। जैसा कि मैंने पहले कहा था, अगर आप अपनी लॉगिन प्रणाली सही ढंग से डिज़ाइन की गई हैं तो आप इसे हश कर सकते हैं - यह इस पर निर्भर करता है कि आप इसे अपनी .php फ़ाइल में कैसे बना रहे हैं।

संबंधित मुद्दे