Issue with Base64-encoded parameter in query string

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 12:12:44

问题


I'm sending a link in my web application to users mails (for confirming user registration) as the following :

<a target="_blank" href="http://localhost:2817/ConfirmRegistration?confirm=Y0tcmGepe7wjH7A1CT1IaA==">
http://localhost:2817/ConfirmRegistration?confirm=Y0tcmGepe7wjH7A1CT1IaA==
</a>

But Chrome alert this message :

Is the query string invalid ? How can I resolve it ?

BTW:
My application is in C# and MVC3


回答1:


You should URL encode the confirm parameter. The error you get is because of the last == fragment.

For this use HttpServerUtility.UrlEncode or similar framework methods.




回答2:


You should probably URL encode the parameter value since = is itself used to separate a parameter name from a parameter value.




回答3:


I was using HttpUtility.UrlEncode but I had problems if the base64 encoded string contained a "+" sign. It was correctly being encoded to "%2b" but when it was coming back from the browser it was interpreted as a space. So, I used two simple encode/decode methods instead:

public static string UrlEncodeBase64(string base64Input)
{
    return base64Input.Replace('+', '.').Replace('/', '_').Replace('=', '-');
}

public static string UrlDecodeBase64(string encodedBase64Input)
{
    return encodedBase64Input.Replace('.', '+').Replace('_', '/').Replace('-', '=');
}



回答4:


You can send your value by replacing two char + to _ and / to -:

string confirm=confirm.Replace('+', '_').Replace('/', '+');

<a target="_blank" href="http://localhost:2817/ConfirmRegistration?confirm=@confirm">
http://localhost:2817/ConfirmRegistration?confirm=@confirm
</a>

and you can get your data in server side by using:

if (Request.QueryString["confirm"] != null && Request.QueryString["confirm"].ToString() != "")
{
       string confirm = HttpUtility.HtmlDecode(Request.QueryString["confirm"]).Replace('_', '+').Replace('-', '/');
}


来源:https://stackoverflow.com/questions/11206259/issue-with-base64-encoded-parameter-in-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!