473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Q about static members in asp.net (C#)

Hi im trying to prevent people from useing more than one browser to view my
page.

But first i need to know excatly how static members of a form class is
behaving.

When is the IntanceCounter (see ***) desctructet?(reset) When the IIS server
is restaret? Then Browser close? Im getting diffrent results everytime i try
this.

I have something like this:
----------------------------
The ASPX
----------------------------
<form id="Form1" method="post" runat="server">
<% Response.Write(WriteHtml()); %>
</form>

----------------------------
codebehind
----------------------------
static int Instancecounter =0; // *** Here is the instanceCounter

public string WriteHtml()
{
if(counter == 0)
{
Instancecounter++;
return "The HTML to render";
}
else
{
return "Only one browser please";
}
}

Nov 18 '05 #1
10 1381
It is generally very different to determine the current active number
of user connected to your web server, unless your users will always
signoff from you website before they exit, and you know that's not
always the case.

When a user close their browser, it does not inform the web server.
You could use javascript to trigger a postback to the web server when
the page unloads, however, it will not always work. One example is
when the user terminate the "Internet Explorer" process in task
manager.

From you code sample, the "IntanceCounter" field is shared among all
objects of the class, so it will not get reset until the ASP.NET
worker process restarts.

There are indirect ways to limit only one browser accessing your
website per machine. However, it will be helpful if you could explain
why you would want to do that.

A web server is designed to serve large amount of requests
simultaneously, and it does this by release the connection from the
client as soon as the request is processed, and that is why it is very
difficult to perform the functionality you're looking for.

Tommy,

"Flare" <None> wrote in message news:<#3**************@TK2MSFTNGP12.phx.gbl>...
Hi im trying to prevent people from useing more than one browser to view my
page.

But first i need to know excatly how static members of a form class is
behaving.

When is the IntanceCounter (see ***) desctructet?(reset) When the IIS server
is restaret? Then Browser close? Im getting diffrent results everytime i try
this.

I have something like this:
----------------------------
The ASPX
----------------------------
<form id="Form1" method="post" runat="server">
<% Response.Write(WriteHtml()); %>
</form>

----------------------------
codebehind
----------------------------
static int Instancecounter =0; // *** Here is the instanceCounter

public string WriteHtml()
{
if(counter == 0)
{
Instancecounter++;
return "The HTML to render";
}
else
{
return "Only one browser please";
}
}

Nov 18 '05 #2
> From you code sample, the "IntanceCounter" field is shared among all
objects of the class, so it will not get reset until the ASP.NET
worker process restarts.
Ok. I forget that idea.
There are indirect ways to limit only one browser accessing your
website per machine. However, it will be helpful if you could explain
why you would want to do that.


Because some guy in the company has used session keys to track servel
navigation infomation and other "intance" specfic detalis. So when the user
open another window he suddenly os manipulation the same sesison object from
2 places. This of course gives bizrar results. Thats why i want a way to
permit only one browser per. user / session.

There is no way of chaning excisting code other than minor changes,,,any
sugestions?

Anders
Nov 18 '05 #3
Hi Flare,

Thank you for posting to the MSDN newsgroups.

I am interested in this issue and researching on it now. I will update you
as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #4
> Thank you for posting to the MSDN newsgroups.

I am interested in this issue and researching on it now. I will update you
as soon as possible.


Thx Jacob.

Anders Jacobsen, Denmark
Nov 18 '05 #5
Hi Anders Jacobsen,

Firstly I want to confirm with you on the static members of a class. A
static member of a class belongs to the type itself rather than to a
specific object. That is, a static member shares among all the instances of
a class in the same process. In this case, the process denotes certain
aspnet_wp.exe, where your web application resides in. only when the
appropriate worker process was recycled will those static members be reset.

As for your concern, you can hook the window close event and then submit a
postback to the server when the window is to be closed. On server side, you
can expire the current session. As a consequence, next time a new session
should be created, instead of using the previous one.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #6
> Firstly I want to confirm with you on the static members of a class. A
static member of a class belongs to the type itself rather than to a
specific object. That is, a static member shares among all the instances of a class in the same process. In this case, the process denotes certain
aspnet_wp.exe, where your web application resides in. only when the
appropriate worker process was recycled will those static members be reset.

K. Nice to clear that out.
As for your concern, you can hook the window close event and then submit a
postback to the server when the window is to be closed. On server side, you can expire the current session. As a consequence, next time a new session
should be created, instead of using the previous one.


But wouldnt that destroy the current session? I mean I surfe the site, then
press CTRL-N to open a new window. Would that "Kill" the current session. I
want the ONLY the curent sesision, and that session should only be avaible
in the browser window that opened it. Did I misunderstand your suggestion?

Anders
Nov 18 '05 #7
Hi Anders,

I have done a lot of research on this issue. I agree with you that your
concern is correct. It seems that there is not an easy/direct way to do
what you want.

I have another idea for your reference:

1. Add an ActiveX control/COM compent to the web application or call a
local (client side) application in the web application.

2. On the client side, we can do some programming to make sure that an
ActiveX control/COM component/application only is started one instance.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #8
> I have done a lot of research on this issue. I agree with you that your
concern is correct. It seems that there is not an easy/direct way to do
what you want.


M2 :) And thx for your interrest in the issue. Your other solution dosent
seem very atractive to me, anyway not to our problem. I have started
thinking about if I could use the OnBrowserClose event to decrement a static
counter. But now im on deep water. Can I get in "contact" with the server
side Form objects static members.? And I would be the big trouble if the
OnBrowserClose event didnt fire. Eg. the browser craches. Then the
intanceCounter would stay at 1 and the next time i opened a "fresh" window i
would not be allowed to see my page because the instancer counter still was
one.

Hmmmmmm......

Anders, Denmark,
Elsam Engineering
Nov 18 '05 #9
Hi Anders,

Thank you for your update.

I have done more research regarding this issue and have another idea for
your reference.

We can try to do this with window.open("URL","WindowName") and then use the
new window which has a name.

For example, suppose he has a logon page. That page would call
window.open("HomePage.aspx","MyApp").

Then, each page in the application would have a hidden field and some
client-side script that copies the window.name into the hidden field. The
hidden field gets posted to the server with each POST (but this doesn't
cover GET requests). On the server, verify that the hidden field has the
value "MyApp".

As for the original window, either set its location.href to a "Please close
me" page, or use document.write to print "please close me" onto the screen.

The code would be something like this:

window.open("http://michmo1/ntest1/Continue.aspx","MyApp")
window.location.href = "http://michmo1/ntest1/PleaseClose.aspx"
OR
window.open("http://michmo1/ntest1/Continue.aspx","MyApp")
document.write("Please close this window")

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #10
> window.open("http://michmo1/ntest1/Continue.aspx","MyApp")
window.location.href = "http://michmo1/ntest1/PleaseClose.aspx"
OR
window.open("http://michmo1/ntest1/Continue.aspx","MyApp")
document.write("Please close this window")


Jacob I think this is an excelent solutions to the problem, a little dirty,
but i guess we can agree that this can't be done nice´n´clean. So ill try
implement your suggestion and see how it works. Ill update you if the
outcome went well. Thax again.

Anders, Denmark
Nov 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
6
by: Matt | last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.