472,811 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 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 1364
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; };
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.