直接调用ASPNETDB数据库中(aspnet_Membership_SetPassword)存储过程来修改 用户密码
//来自:http://niunan.javaeye.com/blog/459172
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web.Security;
using System.Data;
public partial class ResetPassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// 重置
protected void btnReset_Click(object sender, EventArgs e)
{
string connStr = WebConfigurationManager.ConnectionStrings["conn"].ToString();
string username = txtUserName.Text.Trim();
if (username.Length==0)
{
Response.Write("请输入用户名!");
return;
}
//=== 产生加密用的密码密钥 ===
string salt = GenerateSalt();
//=== 将明码密码加密(此时密码为"P@ssw0rd" 当然也可随机数生成) ===
string password = EncryptToHashString("123456", salt, "SHA1");
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
//=== 在此我们呼叫 Membership 提供者 数据库里的预存程序来重置密码 ===
SqlCommand cmd = new SqlCommand("aspnet_Membership_SetPassword", conn);
cmd.CommandType = CommandType.StoredProcedure;
//=== 目前使用 Membership 提供者的 web 应用程序名称 ===
cmd.Parameters.Add(new SqlParameter("@ApplicationName", Membership.ApplicationName));
//=== 要重置密码的用户账号 ===
cmd.Parameters.Add(new SqlParameter("@UserName", username));
//=== 加密过的密码 ===
cmd.Parameters.Add(new SqlParameter("@NewPassword", password));
//=== 密码加密密钥(一定和使用加密密码的密钥一样,不要再重新产生) ===
cmd.Parameters.Add(new SqlParameter("@PasswordSalt", salt));
//=== 重置密码的时间 ===
cmd.Parameters.Add(new SqlParameter("@CurrentTimeUtc", DateTime.Now));
//=== 密码加密的格式(此时是Hash1,注意传入参数是int型态。) ===
cmd.Parameters.Add(new SqlParameter("@PasswordFormat", Membership.Provider.PasswordFormat.GetHashCode()));
//=== 宣告一个可以接收回传值得参数 ===
SqlParameter returnValue = new SqlParameter();
returnValue.ParameterName = "returnValue";
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
//=== 执行预存程序 ===
cmd.ExecuteNonQuery();
conn.Close();
//=== 检查重置密码是否成功 ===
if (returnValue.Value.ToString() == "0")
Response.Write("重置密码成功!!");
else
Response.Write("重置密码失败!!");
}
/// <summary>
/// 密码加密钥
/// </summary>
/// <returns></returns>
public string GenerateSalt()
{
byte[] data = new byte[0x10];
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(data);
return Convert.ToBase64String(data);
}
/// <summary>
/// 哈希密码加密(不可还原)
/// </summary>
/// <param name="s">原始字符串</param>
/// <param name="saltKey">Salt加密字符串</param>
/// <param name="hashName">加密格式(MD5, SHA1, SHA256, SHA384, SHA512.)</param>
/// <returns>加密过的密码</returns>
public string EncryptToHashString(string s, string saltKey, string hashName)
{
byte[] src = System.Text.Encoding.Unicode.GetBytes(s);
byte[] saltbuf = Convert.FromBase64String(saltKey);
byte[] dst = new byte[saltbuf.Length + src.Length];
byte[] inArray = null;
System.Buffer.BlockCopy(saltbuf, 0, dst, 0, saltbuf.Length);
System.Buffer.BlockCopy(src, 0, dst, saltbuf.Length, src.Length);
System.Security.Cryptography.HashAlgorithm algorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web.Security;
using System.Data;
public partial class ResetPassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// 重置
protected void btnReset_Click(object sender, EventArgs e)
{
string connStr = WebConfigurationManager.ConnectionStrings["conn"].ToString();
string username = txtUserName.Text.Trim();
if (username.Length==0)
{
Response.Write("请输入用户名!");
return;
}
//=== 产生加密用的密码密钥 ===
string salt = GenerateSalt();
//=== 将明码密码加密(此时密码为"P@ssw0rd" 当然也可随机数生成) ===
string password = EncryptToHashString("123456", salt, "SHA1");
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
//=== 在此我们呼叫 Membership 提供者 数据库里的预存程序来重置密码 ===
SqlCommand cmd = new SqlCommand("aspnet_Membership_SetPassword", conn);
cmd.CommandType = CommandType.StoredProcedure;
//=== 目前使用 Membership 提供者的 web 应用程序名称 ===
cmd.Parameters.Add(new SqlParameter("@ApplicationName", Membership.ApplicationName));
//=== 要重置密码的用户账号 ===
cmd.Parameters.Add(new SqlParameter("@UserName", username));
//=== 加密过的密码 ===
cmd.Parameters.Add(new SqlParameter("@NewPassword", password));
//=== 密码加密密钥(一定和使用加密密码的密钥一样,不要再重新产生) ===
cmd.Parameters.Add(new SqlParameter("@PasswordSalt", salt));
//=== 重置密码的时间 ===
cmd.Parameters.Add(new SqlParameter("@CurrentTimeUtc", DateTime.Now));
//=== 密码加密的格式(此时是Hash1,注意传入参数是int型态。) ===
cmd.Parameters.Add(new SqlParameter("@PasswordFormat", Membership.Provider.PasswordFormat.GetHashCode()));
//=== 宣告一个可以接收回传值得参数 ===
SqlParameter returnValue = new SqlParameter();
returnValue.ParameterName = "returnValue";
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
//=== 执行预存程序 ===
cmd.ExecuteNonQuery();
conn.Close();
//=== 检查重置密码是否成功 ===
if (returnValue.Value.ToString() == "0")
Response.Write("重置密码成功!!");
else
Response.Write("重置密码失败!!");
}
/// <summary>
/// 密码加密钥
/// </summary>
/// <returns></returns>
public string GenerateSalt()
{
byte[] data = new byte[0x10];
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(data);
return Convert.ToBase64String(data);
}
/// <summary>
/// 哈希密码加密(不可还原)
/// </summary>
/// <param name="s">原始字符串</param>
/// <param name="saltKey">Salt加密字符串</param>
/// <param name="hashName">加密格式(MD5, SHA1, SHA256, SHA384, SHA512.)</param>
/// <returns>加密过的密码</returns>
public string EncryptToHashString(string s, string saltKey, string hashName)
{
byte[] src = System.Text.Encoding.Unicode.GetBytes(s);
byte[] saltbuf = Convert.FromBase64String(saltKey);
byte[] dst = new byte[saltbuf.Length + src.Length];
byte[] inArray = null;
System.Buffer.BlockCopy(saltbuf, 0, dst, 0, saltbuf.Length);
System.Buffer.BlockCopy(src, 0, dst, saltbuf.Length, src.Length);
System.Security.Cryptography.HashAlgorithm algorithm = System.Security.Cryptography.HashAlgorithm.Create(hashName);
inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);
}
}