2013年10月21日 星期一

SQLCLR FormsAuthentication.HashPasswordForStoringInConfigFile

人客給了一個EXCEL,要批次先建好系統使用者
但因預設密碼為使用者帳戶,每個人都不同,我又想直接透過SQLServer openrowset 方式讀取excel檔然後直接轉入。

首選當然是建一個CLR Funciton 直接在tsql 使用,但CLR不支援System.Web,所以FormsAuthentication.HashPasswordForStoringInConfigFile 就沒法使用了。

找到可行的解決方法,改寫成CLR Funciton ,記錄一下 ,粉方便~~~

What will give me the same result as FormsAuthentication hashing for a non web application?


public static string SHA1(this string stringToHash)
{
    // SHA1 is 160bit
    SHA1 sha = new SHA1Managed();
    byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(stringToHash));

    StringBuilder stringBuilder = new StringBuilder();
    foreach (byte b in hash)
    {
     stringBuilder.AppendFormat("{0:x2}", b);
    }
    return stringBuilder.ToString().ToUpper(); // HashPasswordForStoringInConfigFile uses uppercase hex
}
 
 

沒有留言:

張貼留言

IIS 網站無法下載中文檔名檔案因為取消高位元字元

 網址+中文檔案名稱方式下載檔案顯示 404 - 找不到檔案或目錄。 但英數字檔案名稱則可下載。 因為設定GCB IIS,其中1項 將高位元字元預設取消勾選了。 27 TWGCB-04-014-0028 要求篩選與其他限制模組 允許高位元字元 這項原則設定決定查詢...