Base-64 字符数组或字符串的长度无效.C# ASP.NET

本文关键字:无效 ASP NET 字符 数组 字符串 Base-64 | 更新日期: 2023-09-27 18:32:04

这是我的加密和解密类

 public class SharedUtility
    {
        public static String Encrypt(string strData)
        {
            if (strData != "")
            {
                strData = string.Format("{0}|{1}", HttpContext.Current.Session.SessionID, strData);
                SHA1Managed shaM = new SHA1Managed();
                Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(strData)));
                Byte[] encByteData;
                encByteData = ASCIIEncoding.ASCII.GetBytes(strData);
                String encStrData = Convert.ToBase64String(encByteData);
                return encStrData;
            }
            else
            {
                return "";
            }
        }
        public static String Decrypt(string strData)
        {
            if (string.IsNullOrEmpty(strData) == false)
            {
                Byte[] decByteData;
                decByteData = Convert.FromBase64String(strData);
                String decStrData = ASCIIEncoding.ASCII.GetString(decByteData);
                String[] SplitValue = decStrData.Split('|');
                String ReturnValue = SplitValue[1];
                return ReturnValue;
            }
            else
            {
                return "";
            }
        } 

这是我的登录页面代码:

 protected void btnSign_Click(object sender, EventArgs e)
    {
        try
        {
            string UserName = TextBoxUserName.Text.Trim().Replace("'", " ");
            string Password = SharedUtility.Encrypt(TextBoxPassword.Text.Trim().Replace("'", " "));
            string Result = WebUsers.AuthenticateUser(UserName, Password);
            if (Result.Contains("Success"))
                Response.Redirect("~/Home/home.aspx", false);
            else
            {
                divResult.Visible = true;
                ResultLabel.Text = Result;
                ResultLabel.Visible = true;
            }
        }
        catch (Exception ex)
        {
            ResultLabel.Text = ex.Message;
        }
    }

}

但它通过以下错误:

Invalid length for a Base-64 char array or string.

注意:我也尝试 Trim()。替换("+", " ");修剪()。替换(" ", "+");

先生,我是使用这样的专业模板的新手,我尝试了越来越多的时间,也尝试在网上搜索,但没有成功解决

 public static DataTable GetUserByUserName(string UserName)
    {
        try
        {
            string ConnectionString = ConfigurationManager.ConnectionStrings["AMLiveConnectionString"].ToString();
            SqlConnection conn = new SqlConnection(ConnectionString);
            SqlCommand cmd = new SqlCommand("WebUsers_GetUsersByUserName", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", UserName);
            DataTable DT = new DataTable();
            SqlDataAdapter Adapter = new SqlDataAdapter(cmd);
            Adapter.Fill(DT);
            return DT;
        }
        catch
        {
            throw;
        }
    }     

Base-64 字符数组或字符串的长度无效.C# ASP.NET

将代码更改为以下内容。希望它能起作用:

 public static DataTable GetUserByUserName(string UserName)
{
    DataTable DT = new DataTable();
    try
    {
        string ConnectionString = ConfigurationManager.ConnectionStrings["AMLiveConnectionString"].ToString();
        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand("WebUsers_GetUsersByUserName", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@UserName", UserName);
        conn.Open();
        SqlDataAdapter Adapter = new SqlDataAdapter(cmd);
        Adapter.Fill(DT);
    }
    catch
    {
        throw;
    }
    finally{
        conn.Close();
        return DT;
    }
}     

变化是你没有打开你的连接,你的回报应该在最终阻止,而不是在尝试