简体   繁体   中英

Invalid length for a Base-64 char array when calling Process.Start on a UrlEncoded URL

I have searched around, but I haven't found exactly the answer to my dilemma.

I have a WCF Web Service written in C# that is called from a .dll also written in C#. This service does some work, then returns a path and querystring portion of a URL. The querystring part of the URL contains a "credential string" which contains a UserID/Password combination. This credential string is first encrypted, then UrlEncoded.

Back at the .dll on the client machine, the partial URL is combined with a hostname to build a complete URL. The complete URL is then passed into a Process.Start(url) method. This causes the default browser to start, and a request made to the URL. The website application decrypts the credential string and uses it to log in the user and start a new session.

The problem arises whenever the encrypted credential string contains a '+' character. Even though the UrlEncode method in the web service encodes the '+' to a '%2b', in the address box of the browser, the '%2b' has reverted back to a '+', which then causes a 'Invalid length for a Base-64 char array' error from the website. This unwanted 'decoding' occurs in all browsers that I am testing (IE, Firefox, Chrome).

Example:

  1. Original plain-text credential string: "MyUserID,MyP@$$w0rd"
  2. Encrypted credential string: "IajZQu8+QcO3az9fORspDRPqFhJeP1xfk2v4XUVhJg0="
  3. UrlEncoded credential string: "IajZQu8%2bQcO3az9fORspDRPqFhJeP1xfk2v4XUVhJg0%3d"

Note the + and = chars in string 2 have been encoded to %2b and %3d in string 3.

I have verified the Process.Start(url) method is passed a complete URL with the credential string UrlEncoded as in 3 above. However, when I look in the address box of the browser, the credential string appears decoded as in 2 above, thus generating the error on the website.

Does anybody have any insight as to why the querystring in my URL gets decoded between the Process.Start() method and when the browser requests the URL? Or what I can do about it?

Thanks for any help.

A tacky workaround to your issue could be to use string.replace() to switch all instances of + for %2b . It does not address the underlying issue, however.

I could not reproduce the behavior you are reporting. Using the following code:

string cipher = "IajZQu8+QcO3az9fORspDRPqFhJeP1xfk2v4XUVhJg0=";
string encoded = HttpUtility.UrlEncode(cipher);
string url = string.Format(@"http://localhost/{0}", encoded);
Process.Start(url);

...my browser spawned with the URL http://localhost/IajZQu8%2bQcO3az9fORspDRPqFhJeP1xfk2v4XUVhJg0%3d , which is precisely what was expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM