How to stop HttpUtility.UrlEncode () from changing + to place?
I am using HttpUtility.UrlEncode()
in a text token, the original token t+Bj/YpH6zE=
when I get HttpUtility.UrlDecode()
it t Bj/YpH6zE=
, which breaks the algorithm. this is the way to stop + changing in space in C #.
I am currently using the replace method to achieve this 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 ");
}
she will 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);
it is a call to RegisterUser from angular js
RegistrationFactory.registerUser = function(user, token){
return $http({method : "post", url:ConfigService.baseUrl+"User/RegisterUser?token="+token, data : user});
};
source to share
No need to stop changing +
to
. If you are using UrlEncode and UrlDecode correctly. 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 the bonus
byte[] buf = Convert.FromBase64String(orgtoken);
source to share
You can use UrlPathEncode method, documentation for UrlEncode method links to the following
You can url encode using UrlEncode method or UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space to a plus (+) character. The UrlPathEncode method converts each space to the string "% 20", which is a hexadecimal space. Use the UrlPathEncode method when you encode a portion of a URL path to ensure a consistent decoded URL, no matter what platform or browser decodes it.
More information is available at http://msdn.microsoft.com/en-us/library/4fkewx0t(v=vs.110).aspx
source to share