473,657 Members | 2,585 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 1400
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**********@d iscussions.micr osoft.com> wrote in message
news:47******** *************** ***********@mic rosoft.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**********@d iscussions.micr osoft.com> wrote in message
news:47******** *************** ***********@mic rosoft.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(txt UserId.Text, txtPassword.Tex t)
mUser = CType(Thread.Cu rrentPrincipal, myUser)

If mUser.Identity. IsAuthenticated = True Then
HttpContext.Cur rent.User = mUser
Session("myPrin cipal") = mUser

Web.Security.Fo rmsAuthenticati on.RedirectFrom LoginPage(txtUs erId.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_AcquireR equestState(ByV al sender As Object, ByVal e As
System.EventArg s) Handles MyBase.AcquireR equestState

If Not Session("myPrin cipal") Is Nothing Then
Thread.CurrentP rincipal = DirectCast(Sess ion("myPrincipa l"), myUser)
HttpContext.Cur rent.User =DirectCast(Ses sion("myPrincip al"), myUser)
Else
If Thread.CurrentP rincipal.Identi ty.IsAuthentica ted = True Then
Web.Security.Fo rmsAuthenticati on.SignOut()
Server.Transfer (Request.Applic ationPath + "/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**********@d iscussions.micr osoft.com> wrote in message
news:47******** *************** ***********@mic rosoft.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
3333
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
2
5674
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 fields to hold values that calculated every time a user opens a page (unique to each session). These static fields are used in other classes thoughout the application. However, it suddenlly struck me that these static fields are (i think) common...
19
24573
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 static so that I don't need an instance in order to call it. However, because my class model is using a common base class, I need to make it abstract too, so that inherited classes are forced to implement it. Am I doing something that can be...
5
1474
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
1453
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
1133
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 session 1 - Static int = 10 session 2 - Static int = 8 If a third session is running does the static interfere with the other sessions, or not. I know maybe my quesxtion is a little bit silly, but i have problems in that kind and ne not sure why.
4
1678
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 do.. Anyone..? Best regards, Christina
2
1375
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. I have this User class which contains some public properties and a number of static methods. The class implements the singelton pattern, which allows a caller to always invoke methods of the User without creating an instance
10
3333
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 = bits = bit_value print "\tBit Array:"
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6167
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.