2010-07-28 18 views
5

मैं लागू कर दिया है मुझे इस का उपयोग करके अपनी asp.net वेबफ़ॉर्म में विकल्प,asp.net "मुझे याद रखें" कुकी

protected void LBtnSubmit_Click(object sender, EventArgs e) 
{ 
    if (this.ChkRememberme != null && this.ChkRememberme.Checked == true) 
    { 
    HttpCookie cookie = new HttpCookie(TxtUserName.Text, TxtPassword.Text); 
    cookie.Expires.AddYears(1); 
    Response.Cookies.Add(cookie); 
    } 
} 

मैं इसे सही तरीके से कर रहा हूँ याद है? कोई सुझाव .. मैं विंडोज प्रमाणीकरण का उपयोग कर रहा हूं और मैं not using asp.net membership हूं ..

उत्तर

11

कुकी में उपयोगकर्ता नाम और पासवर्ड को सीधे संग्रहीत करने के बजाय, उपयोगकर्ता नाम और पासवर्ड का हैश और कुकी में नमक, फिर जब आप कुकी को प्रमाणित करते हैं, तो दिए गए उपयोगकर्ता नाम के लिए पासवर्ड पुनर्प्राप्त करें, फिर से बनाएं हैश पासवर्ड और एक ही नमक के साथ है और उनकी तुलना करें।

हैश बनाना एक स्ट्रिंग में पासवर्ड और नमक मानों को एक साथ संग्रहित करना, स्ट्रिंग को बाइट सरणी में परिवर्तित करना, बाइट सरणी के हैश की गणना करना (एमडी 5 या जो कुछ भी आप पसंद करते हैं) की गणना करना और परिणामी हैश को परिवर्तित करना एक स्ट्रिंग के लिए (शायद बेस 64 एन्कोडिंग के माध्यम से)।

// Create a hash of the given password and salt. 
public string CreateHash(string password, string salt) 
{ 
    // Get a byte array containing the combined password + salt. 
    string authDetails = password + salt; 
    byte[] authBytes = System.Text.Encoding.ASCII.GetBytes(authDetails); 

    // Use MD5 to compute the hash of the byte array, and return the hash as 
    // a Base64-encoded string. 
    var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); 
    byte[] hashedBytes = md5.ComputeHash(authBytes); 
    string hash = Convert.ToBase64String(hashedBytes); 

    return hash; 
} 

// Check to see if the given password and salt hash to the same value 
// as the given hash. 
public bool IsMatchingHash(string password, string salt, string hash) 
{ 
    // Recompute the hash from the given auth details, and compare it to 
    // the hash provided by the cookie. 
    return CreateHash(password, salt) == hash; 
} 

// Create an authentication cookie that stores the username and a hash of 
// the password and salt. 
public HttpCookie CreateAuthCookie(string username, string password, string salt) 
{ 
    // Create the cookie and set its value to the username and a hash of the 
    // password and salt. Use a pipe character as a delimiter so we can 
    // separate these two elements later. 
    HttpCookie cookie = new HttpCookie("YourSiteCookieNameHere"); 
    cookie.Value = username + "|" + CreateHash(password, salt); 
    return cookie; 
} 

// Determine whether the given authentication cookie is valid by 
// extracting the username, retrieving the saved password, recomputing its 
// hash, and comparing the hashes to see if they match. If they match, 
// then this authentication cookie is valid. 
public bool IsValidAuthCookie(HttpCookie cookie, string salt) 
{ 
    // Split the cookie value by the pipe delimiter. 
    string[] values = cookie.Value.Split('|'); 
    if (values.Length != 2) return false; 

    // Retrieve the username and hash from the split values. 
    string username = values[0]; 
    string hash = values[1]; 

    // You'll have to provide your GetPasswordForUser function. 
    string password = GetPasswordForUser(username); 

    // Check the password and salt against the hash. 
    return IsMatchingHash(password, salt, hash); 
} 
+0

@Erik मैंने इन सबको कक्षा में शामिल किया है .. मेरे बटन पर उनका उपयोग कैसे करें? –

+1

मुझे लगता है कि आपका लॉगिन बटन है: उस स्थिति में, उपयोगकर्ता नाम और पासवर्ड प्राप्त करें जैसा कि आप आमतौर पर करेंगे, उपयोगकर्ता नाम, पासवर्ड और नमक में गुजरने वाली 'CreateAuthCookie' विधि को कॉल करें (जो वास्तव में केवल किसी भी मनमानी स्ट्रिंग है, लंबे समय तक जैसा कि आप प्रत्येक विधि कॉल के लिए एक ही उपयोग करते हैं) - फिर उस विधि को करें जो आपको कुकी के साथ पसंद है। –

+1

जब यह देखने का समय आता है कि उपयोगकर्ता पहले से लॉग इन है, तो आप बस अपनी कुकी को नाम ('YourSiteCookieNameHere') से ढूंढें, और उस कुकी में दिए गए वास्तविक प्रमाणीकरण डेटा के साथ उस कुकी में मानों की तुलना करने के लिए 'IsValidAuthCookie' विधि को कॉल करें। डेटाबेस। एक ही नमक का उपयोग करना न भूलें। –

4

मैं कुकीज में उपयोगकर्ता पासवर्ड स्टोर नहीं करता ... बल्कि उपयोगकर्ता आईडी और आईपी पते को कुकी में स्टोर करें।

+4

उपयोगकर्ता को कार्यालय/घर वाईफ़ाई –

+0

से स्थानांतरित करने के लिए फिर से लॉगिन करना होगा –

0

मैं कुकी में आईपी/प्रयोक्ता आईडी की दुकान नहीं होगा:

यहाँ कुछ उदाहरण कोड है। सत्र हाईजैकिंग वास्तव में आसान होगा, मेरा मतलब है कि मैं अपने कॉलेग्यूज़ का उपयोगकर्ता नाम/आईपी जानता हूं, मैं उस कुकी को अपने संदेश में जोड़ सकता हूं और फिर मैं अपने कॉलेग्यू के सत्र पर काम कर सकता हूं।

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