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
}
 
 

沒有留言:

張貼留言

publish error allowDefinition='MachineToApplication'

一個老舊的aspx web form專案,調了一些功能建置成功,但進行部署時顯示以下錯誤。 在應用程式層級之外使用註冊為 allowDefinition='MachineToApplication' 的區段發生錯誤。錯誤的原因可能是虛擬目錄尚未在 IIS 中設定為...