Nick Kahn
Nick Kahn

Reputation: 20078

Reading encoded url in asp.net code behind

I am using this encoding/decoding javascript 64bit from here http://www.webtoolkit.info/javascript-base64.html

so here is the scanrio: from JavaScript i am redireting to a new aspx page and on the page_load i am reading the QueryString id.

Everything working fine but the question is, if i want to encode/decode in asp.net in codebehind how would i do?

i am planning to encode before i redirect to a page from .JS but how would i read the encoded in asp.net code behind?

Upvotes: 1

Views: 2328

Answers (1)

Damith
Damith

Reputation: 63065

Everything working fine but the question is, if i want to encode/decode in asp.net in codebehind how would i do?

use below methods in code behind to encode and decode.

static public string DecodeFrom64(string encodedData)
{
    byte[] encodedDataAsBytes
        = System.Convert.FromBase64String(encodedData);
    string returnValue =
        System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
    return returnValue;
}

static public string EncodeTo64(string toEncode)
{
    byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
    string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
    return returnValue;
}

i am planning to encode before i redirect to a page from .JS but how would i read the encoded in asp.net code behind?

you can call DecodeFrom64 method to decode encoded text using JS.

Test :

 input = "Abu Hamzah"
 JS encoded text = "QWJ1IEhhbXphaA=="

DecodeFrom64("QWJ1IEhhbXphaA==") result is "Abu Hamzah" and EncodeTo64("Abu Hamzah") result is "QWJ1IEhhbXphaA==" as expected.

Edit:

Add base64 encode decode java script

<script type="text/javascript" src="/js/webtoolkit.base64.js">

you can download it from here http://www.webtoolkit.info/djs/webtoolkit.base64.js

in your javascript you can call this method as below

window.location.href="mypage.aspx?id=" + Base64.encode('Test');

if you want to decode this query string parameter from server side, then you can use DecodeFrom64 method

Upvotes: 1

Related Questions