Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2006, 05:51 PM
Registered User
 
Join Date: Nov 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Setting logged in UserName to Session ID

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

 
Old November 28th, 2006, 06:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old November 28th, 2006, 09:33 PM
Registered User
 
Join Date: Nov 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old November 29th, 2006, 10:45 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old November 29th, 2006, 12:15 PM
Authorized User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi , u can use user.identity.name to get the user name

eg .

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

 
Old November 29th, 2006, 12:25 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old November 29th, 2006, 12:36 PM
Authorized User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi IMAR , Sure , Ur are correct. But its still old 'classic asp mind' for many.

 
Old November 29th, 2006, 01:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old November 29th, 2006, 01:58 PM
Registered User
 
Join Date: Nov 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again, I guess it's back to the books.. :)

 
Old November 30th, 2006, 05:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
insert the username of the logged in user cesemj ASP.NET 2.0 Basics 6 February 12th, 2008 10:13 AM
Username of logged in user spardoe BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 August 18th, 2006 01:13 AM
setting a web control's ID programatically lcsgeek General .NET 3 January 24th, 2005 02:50 PM
Logged - in status without using session madhukp Classic ASP Basics 13 July 14th, 2004 05:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.