2012-06-22 12 views
5

मैं एक सीएस प्रमुख हूं और मैंने अभी एक एएसपीनेट साइट तैयार की है, और साइट के लिए मुझे एक लॉगिन प्रमाणीकरण प्रणाली की आवश्यकता है ... मैं SQLMembershipProvider का उपयोग नहीं करना चाहता था क्योंकि मैं वास्तव में सीखना चाहता था अपने आप को कैसे बनाना है ... वैसे भी यही वह है जो मैं आया था, और मैं सोच रहा था कि कोई मुझे कुछ प्रतिक्रिया, टिप्स या सलाह दे सकता है।एएसपीनेट प्रमाणीकरण

अग्रिम धन्यवाद

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Security.Cryptography; 

/// <summary> 
/// Summary description for PwEncrypt 
/// </summary> 
public class PwEncrypt 
{ 
    public const int DefaultSaltSize = 5; 

    private static string CreateSalt() 
    { 
     RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); 
     byte[] buffer = new byte[DefaultSaltSize]; 
     rng.GetBytes(buffer); 
     return Convert.ToBase64String(buffer); 
    } 

    public static string CreateHash(string password, out string salt) 
    { 
     salt = CreateSalt(); 
     string saltAndPassword = String.Concat(password, salt); 
     string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1"); 
     hashedPassword = string.Concat(hashedPassword, salt); 
     return hashedPassword; 
    } 
     public static string CreateHashAndGetSalt(string password, string salt) 
    { 

     string saltAndPassword = String.Concat(password, salt); 
     string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, "SHA1"); 
     hashedPassword = string.Concat(hashedPassword, salt); 
     return hashedPassword; 
    } 

    public static bool comparePassword(string insertedPassword, string incUserName, out string newEncryptedPassword, out string originalPassword) 
    { 
     databaseInteraction DBI = new databaseInteraction(); 
     string actualPassword =""; 
     string salt = ""; 


     DBI.getSaltandPassword(incUserName, out salt, out actualPassword); 
     string hashedIncPassword = PwEncrypt.CreateHashAndGetSalt(insertedPassword, salt); 
     // hashedIncPassword = string.Concat(hashedIncPassword, salt);  
     newEncryptedPassword = hashedIncPassword; 
     originalPassword = actualPassword; 
     if (newEncryptedPassword == originalPassword) 
     { 

      return true; 
     } 

     else { return false; } 

    } 
+0

यदि आप कस्टम सदस्यता प्रदाता बनाना चाहते हैं, तो आपको पहले सदस्यशिपप्रोवाइडर से उत्तराधिकारी होना होगा। और यदि आप थोड़ा सा Google करते हैं तो यह काफी अच्छी तरह से प्रलेखित है। –

+2

अपने स्वयं के कस्टम प्रमाणीकरण और सत्र प्रबंधन योजनाओं को रोल करने का प्रयास न करें या अपने नियंत्रणों का निर्माण न करें जब तक कि आपके पास वास्तव में कोई अन्य विकल्प न हो। मैं बस इसे करना चाहता हूं, योग्य नहीं है। एक दिलचस्प पढ़ा: http://www.troyhunt.com/2010/07/owasp-top-10-for-net-developers-part-3.html –

+0

@ क्रिस्टोफेजर्स बिल्कुल ... यह स्वयं को करने के लिए बहुत बोझिल है, अंतर्निहित सदस्यता प्रदाता अच्छी तरह से काम करते हैं, इसलिए उन्हें ऐसा करने के लिए कॉन्फ़िगर किया जाना चाहिए। – sinni800

उत्तर

1

मान लीजिए हम इस

enter image description here

की तरह खातों के लिए एक मेज है तो फिर तुम कुछ सहायक वर्ग है कि एक खाता वस्तु

private static Account GetByName(string accountName, bool activatedOnly = false) 
{ 
    using (var context = new DBEntities()) 
    { 
     return context.Accounts.FirstOrDefault(s => 
      s.AccountName == accountName && 
      s.IsApproved == activatedOnly); 
    } 
} 

public static Account Get(string accountName, string password) 
{ 
    var account = GetByName(accountName, true); 
    if (account != null) 
     if (!Cryptographer.IsValidPassword(password, 
              account.PasswordSalt, 
              account.PasswordKey)) 
      return null; 
    return account; 
} 

रिटर्न बना सकते हैं मैं EntityFramework का उपयोग कर रहा हूं लेकिन यह यहां महत्वपूर्ण नहीं है। मुख्य विचार यह दिखाता है कि आपको खातों की पूरी सूची प्राप्त करने की आवश्यकता नहीं है (विशेष रूप से यदि आपके पास उपयोगकर्ताओं की एक बड़ी सूची है)।
मेरे Cryptographer वर्ग की तरह

public class Cryptographer 
{ 
    private const int keyByteLength = 20; 
    public static void Encrypt(string password, 
           out byte[] salt, 
           out byte[] key) 
    { 
     using (var deriveBytes = new Rfc2898DeriveBytes(password, 
                 keyByteLength)) 
     { 
      salt = deriveBytes.Salt; 
      key = deriveBytes.GetBytes(keyByteLength); 
     } 
    } 
    public static bool IsValidPassword(string password, 
             byte[] salt, 
             byte[] key) 
    { 
     using (var deriveBytes = new Rfc2898DeriveBytes(password, salt)) 
     { 
      byte[] newKey = deriveBytes.GetBytes(keyByteLength); 
      return newKey.SequenceEqual(key); 
     } 
    } 
} 
cource के

आप अपने खुद के एल्गोरिथ्म लागू कर सकते हैं लग रहा है।

+0

धन्यवाद, यह वास्तव में फीडबैक का प्रकार है जिसे मैं ढूंढ रहा था ... Rfc2898DeriveBytes बनाम RNGCryptoServiceProvider और FormsAuthentication.HashPasswordForStoringInConfigFile का उपयोग करने के संबंध में ... जिसमें मजबूत हैशिंग एल्गोरिदम है? जहां तक ​​उपयोगकर्ताओं की पूरी सूची मिलती है, अन्य विधियां वास्तव में विशिष्ट उपयोगकर्ता को पासवर्ड की जांच करने के लिए संभालती हैं, incUserID केवल उस पासवर्ड को चेक करता है, इसलिए मेरा मतलब है कि यह केवल 1 रिटर्न के साथ पूरे डेटाबेस के खिलाफ चयन करता है लेकिन EntityFramework ठीक वही काम करेगा? – user1474120

+0

@ user1474120 - मुख्य समस्या जो आप करने जा रहे हैं वह सुरक्षित SQL क्वेरी लिख रहा है। अपने कोड के आधार पर, आप एक सुरक्षित पासवर्ड स्कीमा का उपयोग नहीं कर रहे हैं जो एक समस्या है, लेकिन उपयोगकर्ता और पासवर्ड को सुरक्षित तरीके से प्राप्त करना एक बड़ी समस्या है। –

+0

@ user1474120, [इस] पर एक नज़र डालें (http://security.stackexchange.com/questions/2051/is-pbkdf2-based-system-cryptology-rfc2898derivebytes-better-for-unicode-pass) – Shymep

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