0

I am using IHttpHandler to get data from a database for filtering purpose. But, I am facing a problem with some filter values which contain special characters like "&","/" etc.

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

UPDATED

 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) {}
});
}

In the above code $('#hndSelectedValue').val() is a hidden element whose value is using for filter which I have to send to the server for filtering. This value in JSON format.

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

Nazaf Anwar
  • 339
  • 1
  • 8
  • 17
JKANNAN
  • 83
  • 1
  • 10
  • From where do you want to pass such filter values? Could you make a specific example? – tyger Feb 26 '15 at 08:08
  • @tyger I am using an 'ajax call from a javascript function' to call that IHttpHandler. Arguments for this ajax call have special charecters like "&","/", etc. – JKANNAN Feb 26 '15 at 08:47

3 Answers3

0

RESOLVED

**In 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;
}

** In C# Code

 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);
    }
JKANNAN
  • 83
  • 1
  • 10
  • Wow, wow, slow down, you **mustn't use a cryptography to encode and decode values with special symbols** - it's to expensive in meaning of computer resources on both sides: client's and server's. It's bad way. Look at my answer above. – tyger Feb 26 '15 at 10:12
0

You can use standard JavaScript function encodeURIComponent() to encode you value to pass it as GET parameter in URL

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

later in your server code use System.Uri.UnescapeDataString() method to decode it back.

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

In addition see URL Encode a string in jQuery for an AJAX request
and How do I decode HTML that was encoded in JS using encodeURIComponent()?

Community
  • 1
  • 1
tyger
  • 435
  • 5
  • 16
  • Ok @tyger I wil take your suggestion, Thank Yu – JKANNAN Feb 26 '15 at 10:46
  • I have some other issue with same filter because of the Url Length(Requested URI is too LONG). Is there any suggestion? – JKANNAN Feb 26 '15 at 10:48
  • Hm, how long is your filter then? It seems that it have to be send to server side as a POST parameter not GET. See [jQuery.ajax reference](http://api.jquery.com/jquery.ajax/). There is problem with formatting in comments, see my next answer. And it's better to make another question regarding this issue. – tyger Feb 26 '15 at 11:08
0

Ajax request to pass long value to server as POST request:

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

UPDATE:

On the server side get it from request with Request.Form["filter_text"]

tyger
  • 435
  • 5
  • 16
  • I am getting a 'Value cannot be null' error message in Handler ProcessRequest() method where we are taking the filter-test from the request, CODE:- var filter_text = System.Uri.UnescapeDataString(context.Request.QueryString["filter_text"]); – JKANNAN Feb 26 '15 at 11:37
  • @JKANNAN In that case you have to use `context.Request.Form["filter_text"]` to get your value. – tyger Feb 26 '15 at 11:43
  • Try to use Developer tool in your browser something like Network and look at what is being sent in fact to server. – tyger Feb 26 '15 at 15:30