簡體   English   中英

什么是 C# 的 HttpServerUtility.UrlTokenDecode 的 JavaScript 等價物?

[英]What is the JavaScript equivalent of C#'s HttpServerUtility.UrlTokenDecode?

JavaScript 中,如何使用HttpServerUtility.UrlTokenEncode解碼在 C# 中編碼的字符串?

在其他語言中有一些等價物,但我無法用 JS 重寫它們。 任何幫助,將不勝感激。

這是Java版本。

這是Objective-C版本。

更新:

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=

我終於設法將Jeffrey ThomasObjective-c 版本URLTokenDecode轉換為 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;
        }

});

我想那將是 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-string 進行編碼:

var str = "äöüÄÖÜçéèñ";
var b64 = window.btoa(unescape(encodeURIComponent(str)))
console.log(b64);

似乎 UrlTokenDecode 有點復雜。
100% 確定的最佳方法是使用 AJAX 在服務器端調用 UrlTokenDecode,然后返回一個簡單的 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);
}

來源

這可能違反發布規則,因為它不是問題的答案,但是,我在尋找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.UrlTokenEncodeHttpServerUtility.UrlTokenEncode

在 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="

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM