Subject: Setting logged in UserName to Session ID
Posted By: Admiral1701 Post Date: 11/28/2006 4:51:32 PM
Hi, I'm new to ASP.NET but have been using classic asp for years.  Anyway...

How do I set the logged in username to the Session ID?  I want to track the user over several pages so they can update their own information in several tables.

Essentially, use the SQL Select statement to bind to the session variable to extract data from the table and likewise update or insert into the various tables.

If there is another way to do it using the built-in ASP.NET Log In controls, I'd be happy to hear it.

Thanks

Reply By: Imar Reply Date: 11/28/2006 5:04:09 PM
Hi there,

You can get the user's ID from the MembershipUser's ProviderUserKey.

Take a look at my last post in this thread for an example...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: Admiral1701 Reply Date: 11/28/2006 8:33:49 PM
Thanks Imar for the help.

But I'm wondering, how come I can't just set the session variable from the <asp:loginName> control?

It seems odd that a whole round trip with lots of extra typing is needed to do something so simple?  Or am I not doing it right? Or do I have to wait for ASP.NET 3.0? :)

Thanks again for your help.

Reply By: Imar Reply Date: 11/29/2006 9:45:01 AM
You can't get the user's ID from that control, but you could access the user's name if you want. So, you could do what you want but use the name instead.

However, that way you're sort of bypassing the features that .NET offers you. The GetUser() method I mentioned always gives you access to the currently logged on user. That way, you don't need to keep track of the user in your own session variables. Isn't that much easier?

You could shield the two lines of code in a single helper property that returns the user object for you or you could combine the two lines of code in a single call. Then you'll have pretty much the same code as you would use when using a session variable.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: SriRamaKrishna Reply Date: 11/29/2006 11:15:44 AM
Hi , u can use user.identity.name to get the user name

eg .

session['username'] = user.identity.name

Reply By: Imar Reply Date: 11/29/2006 11:25:27 AM
Either way, why would you want to store that value in a session variable? What's the point in creating additional session variables when you can access the user's name from the Identity object or from a MembershipUser object??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: SriRamaKrishna Reply Date: 11/29/2006 11:36:04 AM
Hi IMAR , Sure , Ur are  correct. But its still old 'classic asp mind' for many.

Reply By: Imar Reply Date: 11/29/2006 12:11:23 PM
I agree. That makes it even more important to show how you can accomplish the same thing using new techniques.... ;-)

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: Admiral1701 Reply Date: 11/29/2006 12:58:16 PM
Thanks again, I guess it's back to the books..  :)

Reply By: Imar Reply Date: 11/30/2006 4:04:38 PM
Good luck. You know where to come to when you need more help... ;-)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: kherrerab Reply Date: 12/1/2006 9:35:37 AM
in global.aspx add
session.add("UserVariable","UserValue")

to set te value just call it with: session.add("UserVariable","yourvaluehere")

then to get it from any part
user = session.item("UserVariable")



Go to topic 26382

Return to index page 105
Return to index page 104
Return to index page 103
Return to index page 102
Return to index page 101
Return to index page 100
Return to index page 99
Return to index page 98
Return to index page 97
Return to index page 96