C# 的 HttpServerUtility.UrlTokenDecode 的 JavaScript 等价物是什么?
What is the JavaScript equivalent of C#'s HttpServerUtility.UrlTokenDecode?
在 JavaScript 中,我如何解码使用 HttpServerUtility.UrlTokenEncode
在 C# 中编码的字符串?
其他语言中有一些等价物,但我无法用 JS 重写它们。任何帮助将不胜感激。
这是 Java 版本。
这是 版本。
更新:
C#的URLTokenEncode
与base64编码不一样。例如,最后一个字符始终是填充字符的数量。所以有些字符需要适当替换。 Java 和 Objective-C 版本的代码显示哪些字符需要用什么替换。
我尝试了decodeURIComponent
,但没有成功解码。这是有道理的,因为字符串是由特定方法编码的。不是base64。
例如,这只是 C# 的 UrlTokenEncode
字符串的一部分:
vj4_fv7__7-_Pr6-ff3_Pr6_vz8_________________f____79_vP1_vb3_vz8____________________AAA1
这是使用Objective-C/Java方法的正确解码版本:
vj4/fv7//7+/Pr6+ff3/Pr6/vz8/////////////////f////79/vP1/vb3/vz8////////////////////AAA=
我想应该是 decodeuricomponent:
http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp
解码 JavaScript 中的 UTF8-base64 编码字符串:
var str2 = decodeURIComponent(escape(window.atob(b64)));
console.log(str2);
在 JavaScript 中对 UTF8-JavaScript 字符串进行编码:
var str = "äöüÄÖÜçéèñ";
var b64 = window.btoa(unescape(encodeURIComponent(str)))
console.log(b64);
UrlTokenDecode 好像有点复杂。
100% 确定的最佳方法是使用 AJAX 在服务器端调用 UrlTokenDecode,然后 return 一个简单的 base64 编码字符串。
public static byte[] UrlTokenDecode (string input)
{
if (input == null)
throw new ArgumentNullException ("input");
if (input.Length < 1)
return new byte[0];
byte[] bytes = Encoding.ASCII.GetBytes (input);
int inputLength = input.Length - 1;
int equalsCount = (int)(((char)bytes[inputLength]) - 0x30);
char[] ret = new char[inputLength + equalsCount];
int i = 0;
for (; i < inputLength; i++) {
switch ((char)bytes[i]) {
case '-':
ret[i] = '+';
break;
case '_':
ret[i] = '/';
break;
default:
ret[i] = (char)bytes[i];
break;
}
}
while (equalsCount > 0) {
ret[i++] = '=';
equalsCount--;
}
return Convert.FromBase64CharArray (ret, 0, ret.Length);
}
阅读 MDN 中描述问题和解决方案的这篇文章:
https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
b64EncodeUnicode('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
我终于设法将 of URLTokenDecode
by Jeffrey Thomas 转换为 JavaScript 并且成功了。
函数如下:
function URLTokenDecode(token) {
if (token.length == 0) return null;
// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);
// The Base64 string is the token without the last character.
token = token.slice(0, -1);
// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');
// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";
return token;
}
如果您使用 AngularJS,这里是 $filter:
app.filter('URLTokenDecode', function () {
return function (token) {
if (token.length == 0) return null;
// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);
// The Base64 string is the token without the last character.
token = token.slice(0, -1);
// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');
// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";
return token;
}
});
这可能违反了发布规则,因为它不是问题的答案,但是,我在寻找 UrlTokenEncode
方法(不是解码)时找到了这个页面,所以使用这里的信息我做了以下方法我希望能帮助别人:
function urlTokenEncode(str) {
var b64 = btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function(match, t) {
return String.fromCharCode('0x' + t);
})),
padChars = b64.match(/=/g);
return b64.replace(/=/g, "") + (padChars == null ? 0 : padChars.length);
}
已测试并使用 C# HttpServerUtility.UrlTokenEncode
和 HttpServerUtility.UrlTokenEncode
在 JavaScript 中,我如何解码使用 HttpServerUtility.UrlTokenEncode
在 C# 中编码的字符串?
其他语言中有一些等价物,但我无法用 JS 重写它们。任何帮助将不胜感激。
这是 Java 版本。
这是
更新:
C#的URLTokenEncode
与base64编码不一样。例如,最后一个字符始终是填充字符的数量。所以有些字符需要适当替换。 Java 和 Objective-C 版本的代码显示哪些字符需要用什么替换。
我尝试了decodeURIComponent
,但没有成功解码。这是有道理的,因为字符串是由特定方法编码的。不是base64。
例如,这只是 C# 的 UrlTokenEncode
字符串的一部分:
vj4_fv7__7-_Pr6-ff3_Pr6_vz8_________________f____79_vP1_vb3_vz8____________________AAA1
这是使用Objective-C/Java方法的正确解码版本:
vj4/fv7//7+/Pr6+ff3/Pr6/vz8/////////////////f////79/vP1/vb3/vz8////////////////////AAA=
我想应该是 decodeuricomponent:
http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp
解码 JavaScript 中的 UTF8-base64 编码字符串:
var str2 = decodeURIComponent(escape(window.atob(b64)));
console.log(str2);
在 JavaScript 中对 UTF8-JavaScript 字符串进行编码:
var str = "äöüÄÖÜçéèñ";
var b64 = window.btoa(unescape(encodeURIComponent(str)))
console.log(b64);
UrlTokenDecode 好像有点复杂。
100% 确定的最佳方法是使用 AJAX 在服务器端调用 UrlTokenDecode,然后 return 一个简单的 base64 编码字符串。
public static byte[] UrlTokenDecode (string input)
{
if (input == null)
throw new ArgumentNullException ("input");
if (input.Length < 1)
return new byte[0];
byte[] bytes = Encoding.ASCII.GetBytes (input);
int inputLength = input.Length - 1;
int equalsCount = (int)(((char)bytes[inputLength]) - 0x30);
char[] ret = new char[inputLength + equalsCount];
int i = 0;
for (; i < inputLength; i++) {
switch ((char)bytes[i]) {
case '-':
ret[i] = '+';
break;
case '_':
ret[i] = '/';
break;
default:
ret[i] = (char)bytes[i];
break;
}
}
while (equalsCount > 0) {
ret[i++] = '=';
equalsCount--;
}
return Convert.FromBase64CharArray (ret, 0, ret.Length);
}
阅读 MDN 中描述问题和解决方案的这篇文章:
https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
b64EncodeUnicode('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
我终于设法将 URLTokenDecode
by Jeffrey Thomas 转换为 JavaScript 并且成功了。
函数如下:
function URLTokenDecode(token) {
if (token.length == 0) return null;
// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);
// The Base64 string is the token without the last character.
token = token.slice(0, -1);
// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');
// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";
return token;
}
如果您使用 AngularJS,这里是 $filter:
app.filter('URLTokenDecode', function () {
return function (token) {
if (token.length == 0) return null;
// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);
// The Base64 string is the token without the last character.
token = token.slice(0, -1);
// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');
// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";
return token;
}
});
这可能违反了发布规则,因为它不是问题的答案,但是,我在寻找 UrlTokenEncode
方法(不是解码)时找到了这个页面,所以使用这里的信息我做了以下方法我希望能帮助别人:
function urlTokenEncode(str) {
var b64 = btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function(match, t) {
return String.fromCharCode('0x' + t);
})),
padChars = b64.match(/=/g);
return b64.replace(/=/g, "") + (padChars == null ? 0 : padChars.length);
}
已测试并使用 C# HttpServerUtility.UrlTokenEncode
和 HttpServerUtility.UrlTokenEncode