IHttpHandler Urls 中的编码/加密

Encoding / Encryption in IHttpHandler Urls

我正在使用 IHttpHandlerdatabase 获取数据以用于过滤目的。但是,我遇到了一些过滤器值的问题,这些值包含 "&""/" 等特殊字符

How can I pass those Filter values with Special Characters to Process Request?

已更新

 function filter_Click(element_clicked) {
 var colName = $(element_clicked).attr("data-id");
     $.ajax({

        type: "GET",
        url: "../../Handlers/FilterValueHandler.ashx?ColumnName=" + colName + "&filter_text=" + $('#hndSelectedValue').val(),
        processData: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (response) {}
});
}

在上面的代码中,$('#hndSelectedValue').val() 是一个隐藏元素,其值用于过滤器,我必须将其发送到服务器进行过滤。此值在 JSON format.

How can I pass those Filter values with Special Characters to Process Request?

已解决

**在javascript

function EncryptText(toEcode) {
    var Key = CryptoJS.enc.Utf8.parse("AMINHAKEYTEM32NYTES1234567891234");
    var IV = CryptoJS.enc.Utf8.parse("7061737323313233");
    var encryptedText = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(toEcode), Key, {keySize: 128 / 8, iv: IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    return encryptedText;
}

** 在 C# 代码中

 public static String AES_decrypt(string encrypted)
    {
        encrypted = encrypted.Replace(' ', '+');// This line resolved my Issue
        var Key = Encoding.UTF8.GetBytes("AMINHAKEYTEM32NYTES1234567891234");
        var IV = Encoding.UTF8.GetBytes("7061737323313233");
        byte[] encryptedBytes = Convert.FromBase64String(encrypted);
        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.BlockSize = 128;
        aes.KeySize = 256;
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;
        aes.Key = Key;
        aes.IV = IV;
        ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
        byte[] secret = crypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
        crypto.Dispose();
        return System.Text.ASCIIEncoding.ASCII.GetString(secret);
    }

您可以使用标准 JavaScript 函数 encodeURIComponent() 对您的值进行编码,以将其作为 GET 参数传递给 URL

//skipped
url: "../../Handlers/FilterValueHandler.ashx?ColumnName=" + colName + "&filter_text=" + encodeURIComponent($('#hndSelectedValue').val()),

稍后在您的服务器代码中使用 System.Uri.UnescapeDataString() 方法对其进行解码。

var decodedValue = System.Uri.UnescapeDataString(Request.QueryString["filter_text"]);

另外见URL Encode a string in jQuery for an AJAX request
How do I decode HTML that was encoded in JS using encodeURIComponent()?

Ajax 请求将 long 值作为 POST 请求传递给服务器:

$.ajax({
    type: "POST",
    url: "../../Handlers/FilterValueHandler.ashx?ColumnName=" + colName,
    data: {"filter_text":$('#hndSelectedValue').val()},
    success: function (response) {}
});

更新:

在服务器端通过 Request.Form["filter_text"]

从请求中获取