在.net 4.5下FormsAuthentication.HashPasswordForStoringInConfigFile过时的有关问题
在.net 4.5下FormsAuthentication.HashPasswordForStoringInConfigFile过时的问题
原代码如下:
在.net framework 4.5下,提示ystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile已经过时
恳请高手帮我改一改
有劳了
------解决思路----------------------
HashPasswordForStoringInConfigFile 这个东西实际上就是使用了指定的 md5 计算你传递的skey的hash,然后返回16进制字符串的形式。在这里那个警告可以无视,非要改就自己 new MD5Cng().ComputeHash(Encoding.UTF8.GetBytes(sKey)) 然后转16进制字符串的形式。
------解决思路----------------------
/// <summary>
/// 返回MD5
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Md5(string input)
{
MD5 md5Hash = MD5.Create();
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
------解决思路----------------------
原代码如下:
public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
在.net framework 4.5下,提示ystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile已经过时
恳请高手帮我改一改
有劳了
------解决思路----------------------
HashPasswordForStoringInConfigFile 这个东西实际上就是使用了指定的 md5 计算你传递的skey的hash,然后返回16进制字符串的形式。在这里那个警告可以无视,非要改就自己 new MD5Cng().ComputeHash(Encoding.UTF8.GetBytes(sKey)) 然后转16进制字符串的形式。
------解决思路----------------------
/// <summary>
/// 返回MD5
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Md5(string input)
{
MD5 md5Hash = MD5.Create();
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
------解决思路----------------------
public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(sKey.Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
public static string MD5(string str)
{
//微软md5方法参考return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
byte[] b = Encoding.Default.GetBytes(str);
b = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = "";
for (int i = 0; i < b.Length; i++)
ret += b[i].ToString("x").PadLeft(2, '0');
return ret;
}