无效的 Base-64 字符数组长度。
我在这里阅读了相关问题,似乎建议如果 ViewState 很大,则将其存储在 SQL 中。 我正在使用带有大量数据收集的向导,因此我的 ViewState 很可能很大。 但是,在我采取“存储在数据库中”的解决方案之前,也许有人可以看一下并告诉我是否有其他选项?
我使用以下方法构造邮件以进行传送:
public void SendEmailAddressVerificationEmail(string userName, string to)
{
string msg = "Please click on the link below or paste it into a browser to verify your email account.<BR><BR>" +
"<a href=\"" + _configuration.RootURL + "Accounts/VerifyEmail.aspx?a=" +
userName.Encrypt("verify") + "\">" +
_configuration.RootURL + "Accounts/VerifyEmail.aspx?a=" +
userName.Encrypt("verify") + "</a>";
SendEmail(to, "", "", "Account created! Email verification required.", msg);
}
Encrypt方法看起来像这样:
public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
以下是 Hotmail 中的 HTML 代码:
请单击下面的链接或将其粘贴到浏览器中以验证您的电子邮件帐户。
http://localhost:1563/Accounts/VerifyEmail.aspx?a=YOHY57xYRENEOu3H+FGq1Rf09AZAI56EPjfwuK8XWKg=
在接收端,VerifyEmail.aspx.cs 页面有以下代码:
string username = Cryptography.Decrypt(_webContext.UserNameToVerify, "verify");
这里是获取UserNameToVerify的getter函数:
public string UserNameToVerify
{
get
{
return GetQueryStringValue("a").ToString();
}
}
这里是GetQueryStringValue方法:
private static string GetQueryStringValue(string key)
{
return HttpContext.Current.Request.QueryString.Get(key);
}
而且decrypt方法看起来像:
public static string Decrypt(string cipherText, string password)
{
**// THE ERROR IS THROWN HERE!!**
byte[] cipherBytes = Convert.FromBase64String(cipherText);
这个错误能够通过代码修复吗,还是我必须将ViewState存储在数据库中?
a = a + new string('=', (4 - a.Length % 4) % 4)
。解码RFC 4648 URL安全的Base64的示例:public string base64urlDecode(string encoded) { return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(encoded.Replace("_","/").Replace("-","+") + new string('=', (4 - encoded.Length % 4) % 4))); }
。 - gregmacUrlDecode
,这导致一些字符被删除。感谢@MattEllen。 - GJKH