[英]How to stop HttpUtility.UrlEncode() from changing + to space?
I am using HttpUtility.UrlEncode()
on a string token the original token is t+Bj/YpH6zE=
when i HttpUtility.UrlDecode()
it becomes t Bj/YpH6zE=
which breaks the algorithm. 我在字符串标记上使用
HttpUtility.UrlEncode()
原始标记是t+Bj/YpH6zE=
当我HttpUtility.UrlDecode()
它变成t Bj/YpH6zE=
这打破了算法。 is a way to stop changing + to a space in c#. 是一种停止在c#中将+更改为空格的方法。
I am currently using replace method to achieve that var token_decrypt = HttpUtility.UrlDecode(token).Replace(" ", "+");
我目前正在使用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 ");
}
her i encode the token 她编码令牌
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);
her is the call of RegisterUser from angular js 她是来自角js的RegisterUser的调用
RegistrationFactory.registerUser = function(user, token){
return $http({method : "post", url:ConfigService.baseUrl+"User/RegisterUser?token="+token, data : user});
};
No need to stop changing +
to 无需停止更改
+
.
。 If you use UrlEncode and UrlDecode correctly.
如果您正确使用UrlEncode和UrlDecode。 Below code works as expected
下面的代码按预期工作
var newtoken = HttpUtility.UrlEncode("t+Bj/YpH6zE=");
//newtoken is t%2bBj%2fYpH6zE%3d now
var orgtoken = HttpUtility.UrlDecode(newtoken);
//orgtoken: t+Bj/YpH6zE=
and for bonus 和奖金
byte[] buf = Convert.FromBase64String(orgtoken);
You can use the UrlPathEncode method, the documentation of UrlEncode method mentions the following 您可以使用UrlPathEncode方法,UrlEncode方法的文档提到以下内容
You can encode a URL using with the UrlEncode method or the UrlPathEncode method.
您可以使用UrlEncode方法或UrlPathEncode方法对URL进行编码。 However, the methods return different results.
但是,这些方法会返回不同的结果。 The UrlEncode method converts each space character to a plus character (+).
UrlEncode方法将每个空格字符转换为加号字符(+)。 The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation.
UrlPathEncode方法将每个空格字符转换为字符串“%20”,该字符串表示十六进制表示法的空格。 Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.
在对URL的路径部分进行编码时使用UrlPathEncode方法,以保证解码的URL一致,无论哪个平台或浏览器执行解码。
More details are available at http://msdn.microsoft.com/en-us/library/4fkewx0t(v=vs.110).aspx 有关详细信息,请访问http://msdn.microsoft.com/en-us/library/4fkewx0t(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.