C# Convert.FromBase64String(...) 抛出 FormatException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17338880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:13:12  来源:igfitidea点击:

Convert.FromBase64String(...) throws a FormatException

c#.netiisbase64iis-express

提问by Dave New

The following line of code runs fine in IIS Express:

以下代码行在IIS Express 中运行良好:

Convert.FromBase64String("dmVoaWNsZUlkPTE0MTM=??");

But when run on my local IIS 8 server, it throws the following exception:

但是当在我的本地IIS 8 服务器上运行时,它会抛出以下异常:

System.FormatException:The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

System.FormatException:输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符或填充字符中的非法字符。

Why is this happening?

为什么会这样?

采纳答案by Samuel Parkinson

The last two characters "??" are not valid in a base 64 string.

最后两个字符 " ??" 在 base 64 字符串中无效。

Have a read here: https://en.wikipedia.org/wiki/Base64

在这里阅读:https: //en.wikipedia.org/wiki/Base64

The string should end in an alphanumeric character or be padded with one or more =characters.

该字符串应以字母数字字符结尾或填充一个或多个=字符。

Edit— Decoding the string without the ?characters returns "vehicleId=1413", so I guess it's just a case of removing them.

编辑- 解码没有?字符的字符串返回“vehicleId=1413”,所以我想这只是删除它们的一种情况。