2012-02-23 54 views
5

我需要使用php對ASP.NET成員資格表進行身份驗證。成員資格api被配置爲使用散列密碼。在PHP中使用ASP.NET成員身份

有人可以好心給我的PHP來散列來自登錄表單的密碼,並將其與sql字段進行比較?

我知道我傳入的密碼是正確的,但不是哈希值相同。

private function Auth($username, $password) 
{ 
    // Hashed password in db 
    $hash = $this->parent->_memberData['conv_password']; 

    // password passed from form 
    $bytes = mb_convert_encoding($password, 'UTF-7');  

    // Salt from db 
    $salt = base64_decode($this->parent->_memberData['misc']); 

    // hash password from form with salt 
    $hashedpassword = base64_encode(sha1($salt . $bytes, true)); 

    // Test em out 
    if ($hashedpassword == $hash) 
    { 
     $this->return_code = "SUCCESS"; 
     return true; 
    } 
    else 
    { 
     $this->return_code = "WRONG_AUTH"; 
     return false; 
    } 
} 

UPDATE:

我已經嘗試不同的編碼有相同的結果。 UTF-7,UTF-8和UTF-16。

更新: 我一直在爲此奮鬥了一個禮拜。賞金來吧...

這裏是單元測試形式的.net代碼。單元測試工作並且值直接從數據庫中取出。這段代碼到php的正確翻譯是什麼?

public void EncodePassword() 
     { 
     string expected = "aP/mqBu3VkX+rIna42ramuosS3s="; 
     string salt = "urIaGX0zd/oBRMDZjc1CKw=="; 
     string pass = "Comeonman"; 

     byte[] bytes = Encoding.Unicode.GetBytes(pass); 
     byte[] numArray = Convert.FromBase64String(salt); 
     byte[] numArray1 = new byte[(int)numArray.Length + (int)bytes.Length]; 
     byte[] numArray2 = null; 
     Buffer.BlockCopy(numArray, 0, numArray1, 0, (int)numArray.Length); 
     Buffer.BlockCopy(bytes, 0, numArray1, (int)numArray.Length, (int)bytes.Length); 

      HashAlgorithm hashAlgorithm = HashAlgorithm.Create("SHA1"); 
      if (hashAlgorithm != null) 
      { 
       numArray2 = hashAlgorithm.ComputeHash(numArray1); 
      } 

     Assert.AreEqual(Convert.ToBase64String(numArray2), expected); 
    } 
+0

你會得到不同的散列值嗎? – MyStream 2012-02-23 05:45:35

+0

是的,哈希不匹配 – Darthg8r 2012-02-23 14:42:19

+0

這可能有所幫助:http://thekindofme.wordpress.com/2008/12/04/aspnet-membership-password-hashing-algorithm/ – Prescott 2012-02-23 17:32:29

回答

6
$password = 'Comeonman'; 
$salt = base64_decode('urIaGX0zd/oBRMDZjc1CKw=='); 
$utf16Password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8'); 
echo base64_encode(sha1($salt.$utf16Password, true)); 
0

密碼是絕對編碼爲UTF-16,以及其鹽是肯定base-64編碼,但我不認爲該散列的結果是base-64編碼。

3

我會建議在.NET中創建一個web服務,它將提供訪問日誌記錄或任​​何其他.NET功能到您的PHP代碼。

然後你可以在內部發送XML請求,看看你得到了什麼。

我相信有一些PHP插件,它可以很容易地調用.NET Web服務。

+0

在這種情況下,這不是一個真正的選擇,因爲我們正在從.net第三方應用程序轉換爲php第三方應用程序。這個想法是最終消除.net應用程序。但你是對的,在許多情況下這不是一個壞主意。 – Darthg8r 2012-03-07 14:54:55

+0

我知道這是一個offtopic,但從.NET遷移到PHP?對我來說似乎很奇怪。 :) – 2012-03-07 15:41:37

0

你不需要machineKey以及解碼/編碼嗎?