I need to transfer a user to a page which requires session data which is stored within a different session.
Is it possible in some cases to migrate the session data by setting the session cookie on the user's browser?
Currently I have no workaround, and using session data seems like the only option I have at the moment.
Is it possible in some cases to
migrate the session data by setting
the session cookie on the user's
browser?
Sure you can. Simply set a new cookie and redirect. You will have to do this server-side. Setting a session cookie client-side will be problematic.
const string sessionStateCookieName = "ASP.NET_SessionId";
string sessionId = "some session id";
HttpCookie cookie = new HttpCookie(sessionStateCookieName, sessionId) { Path = "/", HttpOnly = true };
Response.Cookies.Remove(sessionStateCookieName);
Response.Cookies.Add(cookie);
// force a redirect to complete session switch
Response.Redirect(Request.Path);
You can expand on this. Here is a working example .aspx page. To switch sessions, simply add a url param 'sessionId'.
<%# Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
// this is the method that does the magic
protected override void OnPreInit(EventArgs e)
{
var sessionId = Request["sessionId"];
if (!string.IsNullOrEmpty(sessionId))
{
// this could be found via reflection if different sessionstate cookie name is used in config
const string sessionStateCookieName = "ASP.NET_SessionId";
HttpCookie cookie = new HttpCookie(sessionStateCookieName, sessionId) { Path = "/", HttpOnly = true };
Response.Cookies.Remove(sessionStateCookieName);
Response.Cookies.Add(cookie);
// force a redirect to complete session switch
Response.Redirect(Request.Path);
}
}
// the rest of this script is just demo code
protected void Page_Load(object sender, EventArgs e)
{
CurrentSessionIdLabel.Text = Session.SessionID;
}
protected void SwitchSessionButton_Click(object sender, EventArgs e)
{
// this is the session we wish to switch to
string switchToSessionId = SessionIdTextBox.Text;
Response.Redirect(Request.Path + "?sessionId=" + Server.UrlEncode(switchToSessionId));
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<div>
Current SessionId<br />
<asp:Label ID="CurrentSessionIdLabel" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Desired SessionId"></asp:Label>
<br />
<asp:TextBox ID="SessionIdTextBox" runat="server"></asp:TextBox>
<br />
<asp:Button ID="SwitchSessionButton" runat="server" Text="Switch Session" OnClick="SwitchSessionButton_Click" />
</div>
</form>
</body>
</html>
No, browsers don't share cookies. Although if you use cookieless sessions it wiil work.
If you have a login mechanism, you can just associate the data with the logged-in user (in a database?) instead of putting it in the session.
Related
I have a simple ASP application page added in a SharePoint project just for presentation purposes, so it's ASP web forms page is hosted in SharePoint.
HTML:
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label runat="server">Number 1:</asp:Label>
<asp:TextBox ID="num1" runat="server"></asp:TextBox>
<asp:Label runat="server">Number 2:</asp:Label>
<asp:TextBox ID="num2" runat="server"></asp:TextBox>
<asp:Label runat="server">Result:</asp:Label>
<asp:TextBox ID="res" runat="server"></asp:TextBox>
<asp:Button Text="ADD Numbers" runat="server" OnClick="Unnamed_Click" />
<asp:ListBox runat="server" ID="list" />
<asp:Label runat="server" ID="previousListValue"></asp:Label>
<asp:Label runat="server">Exception:</asp:Label>
<asp:TextBox ID="exception" runat="server"></asp:TextBox>
</asp:Content>
Here is code behind:
public partial class Default : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
list.DataSource = new List<ListItem>() {
new ListItem("value1", "1"),
new ListItem("value2", "2"),
new ListItem("value3", "3"),
};
list.DataBind();
}
}
protected void Unnamed_Click(object sender, EventArgs e)
{
try
{
res.Text = Convert.ToString(int.Parse(num1.Text) + int.Parse(num2.Text));
previousListValue.Text = "Previous list selected value is: " + list.SelectedItem.Value;
exception.Text = string.Empty;
}
catch (Exception ex)
{
exception.Text = ex.GetType().ToString() + "\t" + ex.Message;
}
}
}
When you click "ADD Numbers" button and addition of num1 and num2 textboxes is appended to the res textbox and selected value of ListBox is shown in label next to it. This works flawlessly if correct values are entered for a number and an item is selected in ListBox.
If the page is left (not interacted with) for about 10-15 minutes and "ADD Numbers" button is clicked, NullReferenceException will be thrown for list.SelectedItem.Value and also textBoxes will be empty. What happened is that the application is in postBack state(Page.IsPostBack is true) but viewstate is not restored.
I guess this has something to do with ViewState being moved to distributed cache as of version 2013 but can someone clarify this for me, and suggest me the most effective way to go about this, without changing AppFabric configuration, because 10-15 minutes of ViewState perversion is not acceptable.
As the ViewState is saved in the user's session, it is possible for
ViewState to expire if a page is not posted back within the session
expiration time.
You can configure your session timeout in web.config file (Session timeout in ASP.NET):
<sessionState timeout="60" />
(Where 50 and 60 are minutes)
You can also check if your session expired using this sample code in Master Page's Page_Load method:
if (Session.Contents.Count == 0)
{
//Session dead!
}
else
{
//Session alive!
}
Or save the logged user and test if it's null:
if (Session["userId"] == null)
{
//Session dead!
}
else
{
//Session alive!
}
Ok, I've already learned that in order to keep the dynamic (created) controls and their viewstate in asp.net we must (re)create them in the Page init event, so they already exist within the page's control hierarchy before the view state is loaded.
As it says in this article.
There is no problem to achieve this behaviour if the criteria to create these controls is outside the web environment, for example a database. But what should I do if what I use to decide how many dynamic controls I have to create is actually a value that is in the controls?
I try to explain it with an example, maybe it's clearer:
Let's say that we have a textbox and two buttons. In the textbox I write the number of how many dynamic controls I want to create, for example 4 checkbox. When I hit the button1 the controls should be created. No problem. Then I check some checkboxes and hit the button2 just to fire a postback. Now I should recreate the controls in the page init event, like we said before, in order to maintain the controls and their state.
And here comes the problem. Because of I'm in the init stage I have no viewstate so I'm no able to access the value in the textbox that tells me how many dynamic checkbox should I create.
I thought that storing the value in the session object would do the trick, but it doesn't. The session object is no accessible as well.
Where can I save the value that it'll be accessible from the init event too?
Thanks and sorry for the long post!
First thing - textbox value is not stored/retrieved from view state, you cannot get textbox value from viewstate.
Coming to actual problem, here is the sequence of (imp) events init -> load view state -> bind postback data -> page load. You can retrieve textbox value only after bind postback data event (which actually takes posted data and binds to the textbox control).
In init only option is to use Request.Form{"textboxid"] to get the textbox value.
You are on the right track.
If you use TextBox, you do not need another ViewState to keep track of how many controls has been created, because TextBox control has its own ViewState already.
You can use either Page_Init or Page_Load to load control back.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication2010.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="NumberTextBox" />
<asp:Button runat="server" ID="CreateControlButton"
OnClick="CreateControlButton_Click"
Text="Create Control" />
<br />
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;
namespace WebApplication2010
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
int ids;
if (Int32.TryParse(NumberTextBox.Text, out ids))
{
for (int i = 0; i < ids; i++)
{
Control ctrl = Page.LoadControl("WebUserControl.ascx");
ctrl.ID = i.ToString();
PlaceHolder1.Controls.Add(ctrl);
}
}
}
}
protected void CreateControlButton_Click(object sender, EventArgs e)
{
}
}
}
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl.ascx.cs"
Inherits="WebApplication2010.WebUserControl" %>
<asp:CheckBox runat="server" ID="CheckBox1" />
<asp:Button runat="server" ID="Button1" OnClick="Button_Click"
Text="Post Back" />
<asp:Label runat="server" ID="Label1" />
<br />
using System;
namespace WebApplication2010
{
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Button_Click(object sender, EventArgs e)
{
Label1.Text = " This control was posted back.";
}
}
}
This is the common paradox with page lifecycle when you work with code behind. For example, you save the editing customerid in viewstate but controls need to bind on init to be able to read their posted values.
Best real life solution is to do what bound controls do on postback, call explicilately the LoadPostBackData on IPostBackDataHandler interface that all controls implement, after you have created them in Page Load event.
You should create an extension method for Controls and call it when needed:
control.DataBind();
control.LoadPostedData(); // extension method
I am trying to dynamically add a web user control that contains an AJAX collapsible panel with a Gridview inside the panel when a user clicks on a button. I am able to add a single instance of the user control but when I add additional user controls it throws the following error:
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Extender controls
may not be registered after PreRender.
Source Error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
I am new to ASP.NET Development and the method I am using to handle postbacks is to store the user controls in a list and add them again on prerender method call. I am not sure what to do or if I am handling the creation of the user controls correctly. Any advice is appreciated.
Here is the back end code:
public partial class Test : System.Web.UI.Page
{
private IList<Control> _persistedControls;
private const string PersistedControlsSessionKey = "thispage_persistedcontrols";
static int count = 1;
private IList<Control> PersistedControls()
{
if (_persistedControls == null)
{
if (Session[PersistedControlsSessionKey] == null)
Session[PersistedControlsSessionKey] = new List<Control>();
_persistedControls = Session[PersistedControlsSessionKey] as IList<Control>;
}
return _persistedControls;
}
protected void Page_Load(object sender, EventArgs e)
{
PersistedControls();
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
IList<Control> controlsToGenerate = PersistedControls();
// regenerate dynamically created controls
foreach (var control in controlsToGenerate)
{
MasterPanel.Controls.Add(control);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Control control = LoadControl("~/WebUserControl/UseCaseSetupUserControl.ascx");
control.ID = "uc" + count++.ToString();
MasterPanel.Controls.Add(control);
_persistedControls.Add(control);
MasterPanel.Controls.Add(new LiteralControl("<br />"));
_persistedControls.Add(new LiteralControl("<br />"));
}
}
Here is the ASPX Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
<asp:Panel ID="MasterPanel" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
Create your controls as early as possible in the Page life cycle.
Since you have the PersistedControls on load, that is where you should add them to the controls collection. The best place to add controls in Init.
If you really can't do that then use Load.
If this must be done on an event, then you can add the control there for the first time, but persist it and remember to add it back on Init or Load from the next postback onwards.
If you add controls after PreRender they cannot add any of their data to the ViewState. SaveViewState occurs just before PreRender. A lot of controls rely heavily on the ViewState to function properly. The exception is simply telling you that you should add your control earlier on in the Page lifecycle.
If I host an ASP.NET page with:
<%# Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void btn_Click(object sender, EventArgs e)
{
lbl.Text = HttpContext.Current.Session["a"] == null ?
"null" :
HttpContext.Current.Session["a"].ToString();
}
protected void btn_Click2(object sender, EventArgs e)
{
lbl.Text = HttpContext.Current.Cache["a"] == null ?
"null" :
HttpContext.Current.Cache["a"].ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpContext.Current.Session["a"] = "CBA";
lbl.Text = "assigned Session Variable";
HttpContext.Current.Cache.Add(
"a", "ABC", null,
DateTime.Now.AddHours(2), TimeSpan.Zero,
CacheItemPriority.NotRemovable, null);
}
}
</script>
<html>
<head>
<title>Testing Session</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" runat="server" Text="read Session" OnClick="btn_Click" />
<asp:Button ID="btn2" runat="server" Text="read Cache" OnClick="btn_Click2" />
<hr />
<asp:Label ID="lbl" runat="server" />
</div>
</form>
</body>
</html>
on the first run I do get the assigned Session Variable text, but upon click the Session object is always null
Id there an option I need to turn on/off to use the normal Session Variables ?
works fine on IIS 6.0 and Cassini (under VS 2008 and 2010).
I'm starting to be without ideas on what's going on :o(
Any help is greatly appreciated!
the process of the example page above
More tests shows that this only happens in IE (ie8 in this case), Firefox, Safari, Opera, Chrome they all give the correct "answer"
check the screen cast of the situation
the problem may be the underscores in the domain. remove the _ and see if the same thing happens
Session ID's are maintained via a locally stored cookie by the browser.
Are you running in some sort of protected mode that prevents IE8 from storing cookies? There should be a red warning icon on the status bar.. something along the "...prevented this site from storing a cookie"
I want to create ASP.NET 4.0 dynamic pages loaded from my MS SQL server. Basically, it's a list of locations with information. For example:
Location1 would have the page www.site.com/location/location1.aspx
Location44 would have the page www.site.com/location/location44.aspx
I don't even know where to start with this, URL rewriting maybe?
Url rewriting addresses a different problem than what you are describing.
You can use an HttpHandler that handles requests to the path location and parse the last segment to get your lookup key then simply pass execution off to an .aspx. Although you are passing execution off to a general page, the url will remain as entered.
I will provide an example. Give it a shot. here is a sample project
LocationHandler.cs
using System.IO;
using System.Web;
namespace DBHandler
{
public class LocationHandler : IHttpHandler
{
#region IHttpHandler Members
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
string page = Path.GetFileNameWithoutExtension(request.Url.Segments[request.Url.Segments.Length - 1]);
// for url ~/location/location33.aspx page will be 'location33'
// do something interesting with page, perhaps
context.Server.Execute("~/locations.aspx?locationId=" + context.Server.UrlEncode(page));
}
public bool IsReusable
{
get { return false; }
}
#endregion
}
}
locations.aspx
<%# Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request["locationId"];
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
web.config excerpt
...
<system.web>
...
<httpHandlers>
<add verb="*" path="location/*.*" type="DBHandler.LocationHandler"/>
</httpHandlers>
</system.web>
...