I have an ASP.NET (4.0) webforms application and I would like to know what options I have when it comes to sharing data between users in my web application.
The most obvious solution would be a database table. But are there any alternatives? I've read something about "Application scope", but I'm not sure that it'll be useful for this scenario.
Applications is what you are searching for.
if (Application["CurrentUsers"] == null)
Application["CurrentUsers"] = 0;
Application["CurrentUsers"] += 1;
Just an example for an current online counter.
To react to the lifetime, see the global.asax:
protected void Application_Start(object sender, EventArgs e)
{
Application["CurrentUsers"] = 0;
}
protected void Application_End(object sender, EventArgs e)
{
Application["CurrentUsers"] = null;
}
Application object Or store it in a in-memory database
Related
I have a customized authentication on the system, what I would like is for certain pages (ex. EditingProfile.aspx) to not be accessible through URL navigation.
if a user is authorized: buttons (ex. btnEditingProfile) is enabled and would redirect to EditingProfile.aspx.
is a user isn't authorized: buttons (btnEditingProfile) would be disable. I'm looking for a way to prevent users from accessing EditingProfile.aspx through URL navigation.
The system I'm working on is old so I need a safe way to secure it that wouldn't interfere with other aspects of the system.
MainMenu.aspx
protected void Page_Load(object sender, EventArgs e)
{
//CODE
if (!m_mainController.m_IsAutorized)
btnEditProfile.Enabled = false;
//MORE CODE
}
protected void btnEditProfile_Click(object sender, EventArgs e)
{
queryStrings=SOMEVALUE();
string QueryString = QueryStringEncrypter.GetEncryptedQueryString(queryStrings);
Response.Redirect("ProfileDetails.aspx?" + QueryString, false);
}
In my opinion, each user should has an login session to know their permission.
https://www.codeproject.com/Articles/21474/Easy-way-to-create-secure-ASP-NET-login-using-Sess
Following Tim Nguyen's suggestion I did the following:
in MainMenue.aspx I added
protected void Page_Load(object sender, EventArgs e)
{
//code
if (!m_mainController.m_IsAutorized)
btnEditProfile.Enabled = false;
Session["url"] = Request.UrlReferrer.AbsoluteUri.ToString();
//more code
}
in EditingProfile.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["url"] != null)
{
Response.Redirect(Session["url"].ToString());
}
//code
}
nothing else was changed. The code is running the way I want it to so I thought id update this question in case someone need it.
I use the following code to count the number of currently open sessions in my ASP.NET (2.0/3.5) application (ASMX web service, legacy code), but if it runs long enough the count stops matching the built in performance counters (my count is higher, Session_End seems to not be invoked sometimes). The sessions are InProc. What might I be missing?
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["OpenSessionCount"] = 0;
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["OpenSessionCount"] = (int)Application["OpenSessionCount"] + 1;
Application.UnLock();
/* Set other Session["foo"] = bar data */
}
protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
Application["OpenSessionCount"] = (int)Application["OpenSessionCount"] - 1;
Application.UnLock();
}
}
"Just use performance counters!"
Yes, I'm just asking because I'm curious where I went wrong.
The Session_End is invoked in two situations:
When Session.Abandon() is called
Immediately after the Session expires
If you close the browser, the Seesion_End event fire when Session Expires.
See MSDN lib
Sessions do not actually begin unless you store something in the Session dictionary. So, you won't get a Session_End event for any that don't actually have a session object allocated.
From MSDN:
When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed. If your application requires a static session ID for the entire session, you can either implement the Session_Start method in the application's Global.asax file and store data in the Session object to fix the session ID, or you can use code in another part of your application to explicitly store data in the Session object.
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["OpenSessionCount"] = 0;
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
// Store something every time to ensure the Session object is allocated.
HttpContext.Current.Session["dummy"] = "Foo";
Application["OpenSessionCount"] = (int)Application["OpenSessionCount"] + 1;
Application.UnLock();
/* Set other Session["foo"] = bar data */
}
protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
Application["OpenSessionCount"] = (int)Application["OpenSessionCount"] - 1;
Application.UnLock();
}
}
Reference: ASP.NET: Session.SessionID changes between requests
I've just started learning ASP.NET and I'm facing a problem with getting textbox values. I want to do a simple calculator with only 4 basic operations but what happens is that after I click the + sign and click Go, I see that I didn't store the first number at all. Second number is fine though. Here is a sample of my code.
public partial class deafult : System.Web.UI.Page
{
public TextBox output = new TextBox();
public double temp,tempAdd, calc;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
tempAdd = Convert.ToDouble(output.Text);
output.Text = String.Empty;
}
//User enters another number after clicking Add button then clicks Proc
protected void proc_Click(object sender, EventArgs e)
{
temp = Convert.ToDouble(output.Text);
calc = tempAdd + temp;
output.Text = calc.ToString();
}
}
I debugged and tempAdd is always 0 but I get the number in temp. temp variables and calc is defined public.
You essentially have the problem with all of your variables being re-initialized on load of the page. Unlike winforms, web is stateless.
There are ways of persisting state between refreshes, however. The most obvious choice for your application would be to only go to the server once with the both values and what you want to do with them. ie One button click.
However, for personal edification, it may be worth looking up ViewState. This allows you to store values in an array of sorts and retrieve them even after a refresh.
There are also Session and Application level arrays in ASP.NET that work in similar ways.
Every time you call the page (by events) all your properties is initialized.
Try to do all your logic in one event or store your properties in manager / service / db.
In web (Asp.Net) on every postback properties will get cleared, try to use ViewState or Session variables to hold these values. Refer Asp.Net State Management concepts from MS.
Hope this may help you.
Web controls are State less so you should user session sate to hold the first value then do your stuff...
Ex:-
protected void btnAdd_Click(object sender, EventArgs e)
{
Session["tempAdd"] = output.Text;
output.Text = String.Empty;
}
protected void proc_Click(object sender, EventArgs e)
{
temp = Convert.ToDouble(output.Text);
string oldval=Session["tempAdd"] != null ?Session["tempAdd"].ToString() :"";
if(oldval!="")
tempadd=Convert.ToDouble(oldval);
calc = tempAdd + temp;
output.Text = calc.ToString();
}
Dont know what the problem is wether Value is not being stored or not being retrieved from isolated settings
Page1.Xaml
Here I am storing data
public void Stop_Click(object sender, RoutedEventArgs e)
{
PhoneApplicationService.Current.State["high"] = count;
}
Here I want to retrive it!
Page2.Xaml
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
TP.Text = (string)PhoneApplicationService.Current.State["high"];
}
Store your data like this :
public void Stop_Click(object sender, RoutedEventArgs e)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("high"))
{
settings.Add("high", count);
}
else
{
settings["high"] = count;
}
settings.Save();
}
And then retrieve stored settings data like this :
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("high"))
{
TP.Text = settings["high"].ToString();
}
}
Hope this helps.
This type of saving data is only for "multitasking" purposes such as when user leaves your app but does not close it by back button. Also system can kill the app if it is in background and user opened another apps (I think the limit is 8 apps in background).
Your approach should work fine when app is not closed and then resumed by multitasking menu for example.
If you want to store data for long-term then use IsolatedStorageSettings as "Mak" answered.
You can find more info on MSDN - http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff817008%28v=vs.105%29.aspx
Please tell me basic steps for coding and server coding and steps of server connection for Simple page hit counter using asp C#. Thanks in advance.
An Idea: When the page loads, increment the count for that page, saved in a database table or file.
Some source code and examples for make a page hit counter.
Some of them are old, and I just place them here just for a good "where to start" ideas for a page hit counter.
http://www.asp101.com/samples/counter_db_aspx.asp
http://www.codeproject.com/KB/custom-controls/ewcounter.aspx
http://www.xdevsoftware.com/blog/post/Hit-Counter-for-ASPNET.aspx
In your Global.asax file:
void Application_Start(object sender, EventArgs e)
{
Application["UserCount"] = 0;
}
void Session_Start(object sender, EventArgs e)
{
Application["UserCount"] = Convert.ToInt32( Application["UserCount"].ToString()) + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
Application["UserCount"] = Convert.ToInt32(Application["UserCount"].ToString()) - 1;
}