2011-12-29 39 views

回答

0

是,

只需在用戶數據存儲區中執行查找即可。

例如。

public static string IsUserValid(string username, string password) 
{ 
    if(!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password)) 
    { 

     var allIsGood = true; 
     // query user datastore for username and password 
     // change allIsGood to true or false based on result 

     return allIsGood; 
    } 

    return false; 
} 

編輯:

因爲我使用的加密密碼和MD5哈希值。所以這個輔助類可能會有所幫助

#region Usings 

using System.Security.Cryptography; 
using System.Text; 

#endregion 

namespace Shared.Tools 
{ 
public static class Crypto 
{ 
    public static string CalculateMd5Hash(string input) 
    { 
     // step 1, calculate MD5 hash from input 
     var md5 = System.Security.Cryptography.MD5.Create(); 
     var inputBytes = Encoding.ASCII.GetBytes(input); 
     var hash = md5.ComputeHash(inputBytes); 

     // step 2, convert byte array to hex string 
     var sb = new StringBuilder(); 
     for (var i = 0; i < hash.Length; i++) 
     { 
      sb.Append(hash[i].ToString("x2")); 
     } 
     return sb.ToString(); 
    } 

    public static string GetHashValue(this MD5 hash, string value) 
    { 
     return GetHashValue(hash, Encoding.UTF8.GetBytes(value)); 
    } 

    public static string GetHashValue(this MD5 hash, byte[] value) 
    { 
     var data = hash.ComputeHash(value); 
     var md5 = ""; 
     for (var i = 0; i < data.Length; i++) 
     { 
      md5 += data[i].ToString("x2").ToLowerInvariant(); 
     } 

     return md5; 
    } 

    public static string MD5(this string value) 
    { 
     var algorithm = 
      System.Security.Cryptography.MD5.Create(); 

     var data = Encoding.ASCII.GetBytes(value); 
     data = algorithm.ComputeHash(data); 
     var md5 = ""; 
     for (var i = 0; i < data.Length; i++) 
     { 
      md5 += data[i].ToString("x2").ToLower(); 
     } 

     return md5; 
    } 
} 
} 

public static string IsUserValid(string username, string password) 
{ 
    // using Linq to Entities/Sql 
    return YourDataContext 
      .Users 
      .Where(u => u.UserName == username 
        && u.Password == password.MD5) 
      .FirstOrDefault() != null; 
} 

我還沒有編譯或測試這一點,你可能會爲你的測試方法做這樣的事情,但它應該給你的你要點在之後。

+0

但密碼存儲爲加密,並且aspnet-membership正在處理所有這些。你知道我應該使用什麼特定的存儲過程嗎? – TPR 2011-12-29 02:20:27

+0

是的加密通常是一個MD5哈希。請參閱我的編輯 – Peter 2011-12-30 23:53:47

+1

MD5是非託管資源,應予以處置。 – usr 2011-12-31 22:26:14

相關問題