I want to access session in handler for that i am inheriting "IRequireSessionState" Class. but while evaluating the session it is coming null, but on the web page it has value. i dont know why this is happening my code of handler is:
public void ProcessRequest(HttpContext context)
{
String Name = Common.GetSessionValue("UserId").ToString() ;
// my code.........
Here is my common method to Get Session's value
public static Guid GetSessionValue(string SessionName)
{
Guid returnvalue = Guid.Empty;
if (HttpContext.Current.Session[SessionName] != null)
{
returnvalue = new Guid(HttpContext.Current.Session[SessionName].ToString());
}
else
{
HttpContext.Current.Response.Redirect("Login.aspx");
}
return returnvalue;
}
Please help me. Thanks in advance
You can try this by Exposing the value to the outside world http://forums.asp.net/t/1506625.aspx/1 and then trying to fetch the value in handler using
context.Request.Form["returnvalue"];
You can see the exposed value in firebug AddOn in Mozilla
Related
I am getting route data value in Page_Load but I want to get this value in a class.
string id = Page.RouteData.Values["id"]; // works in Page_Load
I have tried this code in a class but it didn't work. It's returning null. I don't want to check in Page_Load() on every page. I just want to check using a function in a class if it's possible.
RouteData routeData = HttpContext.Current.Request.RequestContext.RouteData;
if(routeData != null)
{
var id = routeData.Values["id"];
}
else
{
// Routedata returning null;
}
Any idea?
Is is possible to retrieve a Session variable from a different controller it was created from?
I create this in my Account controller
System.Web.HttpContext.Current.Session["Session"] = sessionGuid;
and from a different controller I'm trying
if (System.Web.HttpContext.Current.Session["Session"] != null)
{
return Json(System.Web.HttpContext.Current.Session["Session"].ToString());
}
return null;
which always returns null.
as a Test I added some code to my Login controller and in that part i do get the value out of it
if (System.Web.HttpContext.Current.Session["Session"] != null)
{
test = System.Web.HttpContext.Current.Session["Session"].ToString();
}
else
{
test = "no";
}
i always got the actual result, not the"no" part of the code
For some reason its different into how i have to access the session, at the other controller i changed to this code and it worked
if (HttpContext.Session["Session"] != null)
{
return Json(HttpContext.Session["Session"].ToString());
}
return null;
if controller is within the same website/virtual directory then YES, however if you are saying your session was created in a different site and you trying to access it within a controller which belongs to different site then you cannot.
I created session to move data between pages using c# asp.net but the result does not appear and the program does not give me error in the code
first page code:
Session["New1"] = desc1.Text;
to send data to Label in Second page
code:
var userType = (string)Session["New1"];
if (userType != null)
{
Label1.Text = userType.ToString() ;
}
else
{
// test "2" etc
}
Try this,
if (Session["New1"]!= null)
{
Label1.Text = Session["New1"].ToString() ;
}
else
{
// test "2" etc
}
Try explicitly checking of your Session variable exists before attempting to use it to avoid any null-reference issues :
// Explicitly check that it exists
if (Session["New1"] != null)
{
// Then grab it (if it is a non-string type, then you can use as to cast it
// (e.g. a List might use Session["List"] as List<Widget>;)
Label1.Text = Convert.ToString(Session["New1"]);
}
else
{
// Do something here
}
This assumes that your value will be set prior to this code getting called. Additionally, any hiccups to the web server (e.g. timeouts, restarts, major exceptions, etc.) will clear all of the values within the Session.
Hi I am trying to develop a functionality that will track everytime there is a new session created in a web app aka a user logs in.
I have created a class called "StateBag.cs"
using System;
using System.Text;
[Serializable()]
public class StateBag
{
#region Business Methods
// To catch the event of a New Session //
private bool _NewSession = false;
public bool NewSession
{
get { return _NewSession; }
set { _NewSession = value; }
}
#endregion
}
On the login page, just before login:-
// Declaration Region. //
private StateBag _Bag;
if (Session.IsNewSession)
{
_Bag = new StateBag();
_Bag.NewSession = true;
// ViewState["StateBag"] = _Bag;
Session["NewSession"] = _Bag;
}
On the Main page, after a successful login:-
// Declaration region. //
StateBag _Bag
{
get
{
return (StateBag)Session["NewSession"];
}
}
if (_Bag.NewSession == true)
{
// Do my stuff........ //
_Bag.NewSession = false; // set new Session back to false//
}
I m having problems retrieving _Bag... it comes back as Null...
hence an error message :-
"Object reference not set to an instance of an object."
Can anyone help me retrieve the NewSession property which I set to "True" on the login page?
You're storing it in ViewState:
ViewState["StateBag"] = _Bag;
And retrieving it from Session:
return (StateBag)Session["NewSession"];
ViewState and Session are two completely different things, they don't share the same objects. You need to pick one place to persist the data and always retrieve it from that same place.
Note: ViewState renders data to the client, so I wouldn't suggest using that to store anything that you don't want a client to be able to see/modify.
I have a class that handles all of my session variables in my asp.net application. Moreover, I sometimes store objects in the session variables so as to allow me to code very similar to a normal .net application.
For example, here is an object in a session variable:
public cUser User
{
get { return (cUser)HttpContext.Current.Session["CurrentUser"]; }
set { HttpContext.Current.Session["CurrentUser"] = value; }
}
And here is the instance of MySessionClass:
public static MySessinClass Current
{
get
{
MySessionClass session = (MySessionClass)HttpContext.Current.Session["MySessionClass"];
if (session == null) {
session = new MySessionClass();
HttpContext.Current.Session["MySessionClass"] = session;
}
return session;
}
}
So, in my code behind aspx page, I would just do something such as
int userID = MySessionClass.Current.User.UserID;
This works great and I love it. However, I want to apply the same principle in javascript from my aspx page:
var userID = <%=MySessionClass.Current.User.UserID%>;
However, when I access that webpage, I get an error saying that it does not recognize the MySessionClass object.
So, given the context that I am in, what would be the best way to reference that UserID variable, from the object, from the session, in javascript? Maybe my syntax to reference it is off or I am going at it the wrong way. Any help would be appreciated, thank you.
Since you've defined it as a static member, you would need to qualify your reference with the page type:
var userID = <%=MyPageType.MySessionClass.Current.User.UserID%>;
That should be enough.