我正在使用HttpUtility.UrlEncode()字符串令牌,原始令牌是t+Bj/YpH6zE=当HttpUtility.UrlDecode()它变为t Bj/YpH6zE=打破算法时.是一种停止在c#中将+更改为空格的方法.
我目前正在使用replace方法来实现这一点 var token_decrypt = HttpUtility.UrlDecode(token).Replace(" ", "+");
public HttpResponseMessage RegisterUser(User user, string token)
{
int accID;
if(string.IsNullOrWhiteSpace(token))
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
else
{
try
{
// token now = t Bj/YpH6zE= which will fail
var token_decrypt = HttpUtility.UrlDecode(token);
token now = t Bj/YpH6zE= still the same
accID = int.Parse(Crypto.Decrypt(token_decrypt, passPhrase));
}
catch
{
return Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid account ");
}
Run Code Online (Sandbox Code Playgroud)
她编码令牌
string encoded_token = HttpUtility.UrlEncode(token);
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
msg.To.Add(mail);
msg.From = new System.Net.Mail.MailAddress(from);
Run Code Online (Sandbox Code Playgroud)
她是来自角js的RegisterUser的调用
RegistrationFactory.registerUser = function(user, token){
return $http({method : "post", url:ConfigService.baseUrl+"User/RegisterUser?token="+token, data : user});
};
Run Code Online (Sandbox Code Playgroud)
无需停止改变+来.如果您正确使用UrlEncode和UrlDecode.下面的代码按预期工作
var newtoken = HttpUtility.UrlEncode("t+Bj/YpH6zE=");
//newtoken is t%2bBj%2fYpH6zE%3d now
var orgtoken = HttpUtility.UrlDecode(newtoken);
//orgtoken: t+Bj/YpH6zE=
Run Code Online (Sandbox Code Playgroud)
和奖金
byte[] buf = Convert.FromBase64String(orgtoken);
Run Code Online (Sandbox Code Playgroud)
您可以使用 UrlPathEncode 方法,UrlEncode 方法的文档中提到了以下内容
您可以使用 UrlEncode 方法或 UrlPathEncode 方法对 URL 进行编码。但是,这些方法返回不同的结果。UrlEncode 方法将每个空格字符转换为加号字符 (+)。UrlPathEncode 方法将每个空格字符转换为字符串“%20”,它以十六进制表示法表示一个空格。在对 URL 的路径部分进行编码时,请使用 UrlPathEncode 方法,以保证解码后的 URL 一致,无论是哪个平台或浏览器执行解码。
有关更多详细信息,请访问 http://msdn.microsoft.com/en-us/library/4fkewx0t(v=vs.110).aspx
| 归档时间: |
|
| 查看次数: |
3136 次 |
| 最近记录: |