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

  • Thread starter Thread starter Flare
  • Start date Start date
F

Flare

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";
}
}
 
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,
 
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
 
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.
 
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
 
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.
 
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
 
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.
 
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
 
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.
 
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
 
Back
Top