473,513 Members | 2,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sessions and static attributes

I have a question related to ASP.net sessions and static attributes. In an
ASP.net application (MyApp) I have a class called Globals with a public
static string called UserName. Will UserName be unique for each session or
will the value of UserName be the same for all sessions?

For example, John launches MyApp and the UserName is assinged the value
"John". Next, Bob launches MyApp and the UserName is assigned the value
"Bob". Does John have a UserName == "John" while Bob has a UserName == "Bob"
or did John's UserName get overwritten to "Bob"?

Thanks, Danny
Nov 19 '05 #1
3 1393
I think it will be the same for all sessions because the same AppDomain will
be used for all sessions. It will also live until the application is
recycled. I think.
"Danny Crowell" <Da**********@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
I have a question related to ASP.net sessions and static attributes. In an
ASP.net application (MyApp) I have a class called Globals with a public
static string called UserName. Will UserName be unique for each session or
will the value of UserName be the same for all sessions?

For example, John launches MyApp and the UserName is assinged the value
"John". Next, Bob launches MyApp and the UserName is assigned the value
"Bob". Does John have a UserName == "John" while Bob has a UserName == "Bob" or did John's UserName get overwritten to "Bob"?

Thanks, Danny

Nov 19 '05 #2
It will be the same for all users. 'static' keyword means there is one
instance of this object per application. The application in this case is
your whole web application. Each user making a request is just a different
thread in this application.

"Danny Crowell" <Da**********@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
I have a question related to ASP.net sessions and static attributes. In an
ASP.net application (MyApp) I have a class called Globals with a public
static string called UserName. Will UserName be unique for each session or
will the value of UserName be the same for all sessions?

For example, John launches MyApp and the UserName is assinged the value
"John". Next, Bob launches MyApp and the UserName is assigned the value
"Bob". Does John have a UserName == "John" while Bob has a UserName ==
"Bob"
or did John's UserName get overwritten to "Bob"?

Thanks, Danny

Nov 19 '05 #3
The static variable (Shared in VB) is used once for all users.
So John will login and be John.
Then Bob will log in be Bob. But now John is Bob too.

Static variables should be used when all users get assigned the same value.
e.g. some global value.

You should create a Principal and Identity class combination which you can
store in Session for each user.
Then you attach the Principal to the thread for each request and you will
know the current user.
Here is some more info:

Here is one way to handle this:

So you need to use Forms Authentication to authenticate a given UID and
PWD
combination. These values can be in your DB and you need to look them up
and
verify that the typped in values match the ones in the DB. (Note that the
connection string for your DB has nothing to do with this. You use those
credentials to make the connection and take advantage of the connection
pool. You do NOT vary the conenct string with each user as this is a true
scalabilit killer.)

Sample code requires you to have a login method on your Principal class
(which calls your Identity class).

mUser.Login(txtUserId.Text, txtPassword.Text)
mUser = CType(Thread.CurrentPrincipal, myUser)

If mUser.Identity.IsAuthenticated = True Then
HttpContext.Current.User = mUser
Session("myPrincipal") = mUser

Web.Security.FormsAuthentication.RedirectFromLogin Page(txtUserId.Text,
False)
Else
'do something else
End If
I use code like this in my Global.asax file to re-use the principal value
on each hit:

Private Sub Global_AcquireRequestState(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.AcquireRequestState

If Not Session("myPrincipal") Is Nothing Then
Thread.CurrentPrincipal = DirectCast(Session("myPrincipal"), myUser)
HttpContext.Current.User =DirectCast(Session("myPrincipal"), myUser)
Else
If Thread.CurrentPrincipal.Identity.IsAuthenticated = True Then
Web.Security.FormsAuthentication.SignOut()
Server.Transfer(Request.ApplicationPath + "/Login.aspx")
End If
End If

End Sub

Rocky Lhotka explains these concepts very well in his book on Business
Objects.
http://www.lhotka.net/ArticleIndex.a...ea=CSLA%20.NET

--
Joe Fallon


"Danny Crowell" <Da**********@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
I have a question related to ASP.net sessions and static attributes. In an
ASP.net application (MyApp) I have a class called Globals with a public
static string called UserName. Will UserName be unique for each session or
will the value of UserName be the same for all sessions?

For example, John launches MyApp and the UserName is assinged the value
"John". Next, Bob launches MyApp and the UserName is assigned the value
"Bob". Does John have a UserName == "John" while Bob has a UserName ==
"Bob"
or did John's UserName get overwritten to "Bob"?

Thanks, Danny

Nov 19 '05 #4

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

Similar topics

33
3314
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
2
5657
by: Earl Teigrob | last post by:
I am using C# to program ASP.NET. I am using class of static fields to hold all variables that are common to the application, and I am sure that this is fine. However, I was also using some static...
19
24564
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this...
5
1467
by: Ren? Paw Christensen | last post by:
Hi. Considering the following code, I want this call: Something.Method(); To return "Something". public class BaseClass { public static string Method() {
2
1447
by: Pohihihi | last post by:
Actually I am not understanding what you mean by > attribute will not be applicable to the type, but rather, all types you mean it will behave like ?? Thanks, Po
2
1129
by: Harald | last post by:
Hi My question is if and how a static variable remains the same state if more than one session of my web site is running. Is the content of my static different at all sessions or not. For sample...
4
1672
by: Christina N | last post by:
Can anyone give me a code example of how to use a global array to keep track of all active sessions (their sessionid, logontime, etc)? I need a code example, not a prosa description of what to...
2
1364
by: Mikael Östberg | last post by:
Hello all! I have this login function which doesn't work really well. I tried to do extend the built-in functionality in order to store things such as userName and email address in Session. ...
10
3322
by: Michael Yanowitz | last post by:
Is it possible to have a static variable in Python - a local variable in a function that retains its value. For example, suppose I have: def set_bit (bit_index, bit_value): static bits = ...
0
7254
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
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
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...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.