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
}
 
 

沒有留言:

張貼留言

pdf.js 無法顯示部份字

有個檔案在pdf viewer套件中無法顯示內容,但下載檔案後使用工具又可以正常顯示。 本來以為是套件版本太舊的原因,於是去下載pdf viewer套件 https://github.com/mozilla/pdf.js 更新後還是一樣。 覺得應是字型缺漏的問題,於是用PDF-X...