You can create a class like this:
using System;
using System.Web;
namespace MyWeb
{
public class AppSession
{
public static UserInfo LoginUser
{
get
{
if (HttpContext.Current.Session["userinfo"] == null)
{
HttpContext.Current.Session["userinfo"] = new UserInfo();
}
return (UserInfo)HttpContext.Current.Session["userinfo"];
}
set
{
if (value == null)
{
throw new Exception("Value assigned to Session-LoginUser detected.");
}
HttpContext.Current.Session["userinfo"] = value;
}
}
}
}
Then you can access it any where in your application:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AppSession.LoginUser = new UserInfo();
AppSession.LoginUser.ClientID = 1;
AppSession.LoginUser.SessionID = 1;
AppSession.LoginUser.ClientName = "Cl1";
AppSession.LoginUser.UserID = 1;
AppSession.LoginUser.UserName = "Manas";
}
}
}
Then at the second page:
protected void Page_Load(object sender, EventArgs e)
{
if (AppSession.LoginUser == null)
{
Response.Redirect("~/Logout.aspx");
}
}
Now, go ahead and try again. Whenever there is a null value assigned to
AppSession.LoginUser
, an Exception will be thrown. From there you can trace where is the error occur in your code.
By the way, this block of code:
Response.Write("<script>");
Response.Write("var height = screen.height;");
Response.Write("var width = screen.width;");
Response.Write("var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';");
Can be written as this:
Response.Write(@"
<script type=""text/javascript"">
var height = screen.height;
var width = screen.width;
var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';"
);
You won't have to repeat
Response.Write()
for each line.
If you want to escape a double quote symbol
"
you can escape it by typing it twice like this:
""
Another example of using
@""
:
This code:
xmlString += "<root>";
xmlString += "<username>" + loggedin.UserName + "</username>";
xmlString += "<userid>" + loggedin.UserID + "</userid>";
xmlString += "<hosturl>" + ConfigurationManager.AppSettings["HostURL"] + "</hosturl>";
xmlString += "<serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + "</serviceurl>";
xmlString += "<random>" + new System.Random().NextDouble().ToString() + "</random>";
xmlString += "<version>" + ConfigurationManager.AppSettings["TAVersion"] + "</version>";
xmlString += "<sessionid>" + loggedin.SessionID + "</sessionid>";
xmlString += "<clientname>" + loggedin.ClientName + "</clientname>";
xmlString += "<subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + "</subscriptionid>";
xmlString += "<sess>" + Request.Cookies["ASP.NET_SessionId"].Value + "</sess>";
xmlString += "<currentyear>" + System.DateTime.Now.Year.ToString() + "</currentyear>";
xmlString += "</root>";
Can be written as:
xmlString = @"
<root>
<username>" + loggedin.UserName + @"</username>
<userid>" + loggedin.UserID + @"</userid>
<hosturl>" + ConfigurationManager.AppSettings["HostURL"] + @"</hosturl>
<serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + @"</serviceurl>
<random>" + new System.Random().NextDouble().ToString() + @"</random>
<version>" + ConfigurationManager.AppSettings["TAVersion"] + @"</version>
<sessionid>" + loggedin.SessionID + @"</sessionid>
<clientname>" + loggedin.ClientName + @"</clientname>
<subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + @"</subscriptionid>
<sess>" + Request.Cookies["ASP.NET_SessionId"].Value + @"</sess>
<currentyear>" + System.DateTime.Now.Year.ToString() + @"</currentyear>
</root>";
You won't have to repeat "
xmlString +=
" on each line.