Wednesday, August 16, 2017

.Net SHA256 Hash Algorithm

The following function enables you to return SHA256 Hashed string with/without salt key :

public string GetSHA256String(string stringTobeHashed, string salt = null)
        {
            stringTobeHashed = !string.IsNullOrEmpty(salt) ? string.Format("{0}{1}", stringTobeHashed, salt) : stringTobeHashed;
            // ----------------- Encoding --------------------           
            byte[] sha256Bytes = Encoding.Default.GetBytes(stringTobeHashed);
            // -----------------------------------------------

            // ----------------- Create SHA256 ---------------
            SHA256 sha256 = SHA256Managed.Create();
            byte[] hashValue = sha256.ComputeHash(sha256Bytes);
            // -----------------------------------------------

            // ----------------- Hash Value String -----------
            var sha256Str = BitConverter.ToString(hashValue).Replace("-", "");

            // OR -> Convert.ToBase64String(sha256Str); 
            // OR: 
            //string sha256Str = string.Empty;
            //for (int i = 0; i < hashValue.Length; i++)
            //{
            // sha256Str += hashValue[i].ToString("X"); // X -> mean format output to Hexadecimal                                                         
            //}
            // -----------------------------------------------           
            return sha256Str;
        }

Tuesday, August 15, 2017

.Net Core Web App hang on Startup ( IIS Express in Visual Studio 2017 )

.Net Core Web App hang on Startup  ( IIS Express in Visual Studio 2017 )

Solution :
1- Installing "Microsoft.AspNetCore.Server.IISIntegration package"
2- In "Program.cs" file Add :

var host = new WebHostBuilder() .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration() // <<<===== Our line 
    .UseStartup<Startup>()
    .Build();