我在写学位模块的ASP web项目不工作,我要插入一些登录信息到登录表。它运行良好,但是我已经将它作为脚本运行在.aspx文件中,但我需要散列密码,因此我不知道在Code Behind文件之外执行此操作的方式,我移动了SQLDataSource。这是插入,不起作用。的SqlDataSource插入方法在代码隐藏.NET C#
SqlDataSource sqldsInsertPassword = new SqlDataSource();
sqldsInsertPassword.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
sqldsInsertPassword.ProviderName = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
sqldsInsertPassword.InsertCommand = "INSERT INTO login (Password, Email) VALUES (@Password, @Email)";
sqldsInsertPassword.InsertCommandType = SqlDataSourceCommandType.Text;
sqldsInsertPassword.InsertParameters.Add("@Email", txtEmail.Text.ToString().ToLower());
sqldsInsertPassword.InsertParameters.Add("@Password", Convert.ToBase64String(getSHA256(txtPassword.Text.ToString())));
sqldsInsertPassword.Insert();
我没有看到有什么问题,但也许你可以从课程的其他部分看出来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Web.Security;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static byte[] getSHA256(string password)
{
SHA256CryptoServiceProvider sha = new SHA256CryptoServiceProvider();
return sha.ComputeHash(System.Text.Encoding.ASCII.GetBytes(password));
}
protected void btnRegister_Click(object sender, EventArgs e)
{//check email, insert user, SQL command get user ID, insert password
SqlDataReader drExistingUsers = (SqlDataReader)sqldsCheckEmail.Select(DataSourceSelectArguments.Empty);
drExistingUsers.Read();
if (drExistingUsers.HasRows == false)
{
drExistingUsers.Close();
bool fault = false;
try
{
sqldsInsertUser.Insert();
}
catch (Exception error)
{
fault = true;
lblError.Text = "Error: " + error;
}
if (fault == false)
{
try
{
SqlDataSource sqldsInsertPassword = new SqlDataSource();
sqldsInsertPassword.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
sqldsInsertPassword.ProviderName = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
sqldsInsertPassword.InsertCommand = "INSERT INTO login (Password, Email) VALUES (@Password, @Email)";
sqldsInsertPassword.InsertCommandType = SqlDataSourceCommandType.Text;
sqldsInsertPassword.InsertParameters.Add("@Email", txtEmail.Text.ToString().ToLower());
sqldsInsertPassword.InsertParameters.Add("@Password", Convert.ToBase64String(getSHA256(txtPassword.Text.ToString())));
sqldsInsertPassword.Insert();
}
catch (Exception insertError)
{
fault = true;
lblError.Text = "Error: " + insertError;
}
if (fault == false)
Response.Redirect("Login.aspx");
}
}
else
lblError.Text = "Email already exists.";
}
我很欣赏有很多命名空间我可能不需要在那里,但我会稍后收拾整理。
感谢那些谁回复!
你得到一个错误或根本什么都没有发生呢? –
是否抛出异常?它说什么?或者它根本不起作用? –
请将异常的堆栈跟踪放入此问题中。或者,如果它不抛出异常,是什么问题?它只是输出错误的东西? – Doorknob