3

I am using c# and jQuery.

I have below code where I am setting the Session Variable using C# code.

if (!string.IsNullOrEmpty(results))
{
    string[] array = results.Split(',');
    string firstName = array[0];
    string lastName = array[1];
    string activeCardNo = array[2];
    string memberShipTier = array[3];
    string accessToken = array[4];

    Session["skyFirstName"] = firstName.ToString();
    Session["skyLastName"] = lastName.ToString();
    Session["skyActiveCardNo"] = activeCardNo.ToString();
    Session["skyMemberShipTier"] = memberShipTier.ToString();
    Session["boolSignOn"] = "true";
    Response.Redirect(fromPage);
    Response.End();
}

Now I want to read these values (Session["skyFirstName"]) using jQuery so that I can set in my elements. Please suggest.

2
  • 3
    Are you sure? You wanna use a client-side technology to read server-side value?? Commented Jan 13, 2011 at 10:20
  • The same question was asked here: how-to-get-asp-net-session-value-in-jquery-method Commented Jan 13, 2011 at 10:29

4 Answers 4

6

Session values are stored on the server and it is impossible to read them with client side javascript. One way to achieve this would be to expose some server side script or generic handler which would return the corresponding session value given a key and then use jQuery to send an AJAX request to this handler and read the value. You should be aware that by doing this the user can read all his session values. Be warned that exposing the same script for writing session values could be catastrophic from security standpoint.

Here's an example:

public class ReadSession : IHttpHandler, IReadOnlySessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.Write(new JavaScriptSerializer().Serialize(new
        {
            Key = context.Request["key"],
            Value = context.Session[context.Request["key"]]
        }));
    }

    public bool IsReusable 
    { 
        get { return true; } 
    }
}

and then query it:

$.getJSON('/ReadSession.ashx', { key: 'skyFirstName' }, function(result) {
    alert(result.Value);
});
Sign up to request clarification or add additional context in comments.

3 Comments

@MKS, I've added an example using a generic handler.
technically this handler is reusable since you're not storing any request specific data in instance variables.
@ Pauli Østerø, very good point. I've updated my answer. I've also used IReadOnlySessionState to allow for concurrent AJAX requests on this handler as we are only reading session here.
1

jquery runs on the client, which cannot directly access your server-side-session values. one solution is to provide a webservice which returns these values and use the webservice, another one would be to include the values in the page-response as JSON (e.g.) and access them on the client.

Comments

1

You cannot access the session variables with javascript as the session variables are server side rather than client side.

One work around that has already been mentioned is to use ajax to allow the javascript to communicate with the server side. This is fine, but possibly overly complicated for what you need.

Another, simpler solution would be to output the session variables into hidden input fields or as javascript variables in script tags which you can then access with the javascript.

Comments

0

jQuery is javascript, so in order to have those variables available you need to print out html code (at least one script tag) where you set the read-out session variables from C# as javascript variable.

An alternative would be to use an ajax request to get the session variables from a server script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.