Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 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 August 11th, 2006, 08:00 AM
Authorized User
 
Join Date: May 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Username of logged in user

I am trying to access the username of the currently logged on user in a web page. I have noticed the following 2 ways:
  this.User.Identity.Name
  HttpContext.Current.User.Identity.Name

What is the difference between these? Also I want to access the name of the currently logged on user from a Master page but neither of the above work. How can I do this?
Thanks.
 
Old August 14th, 2006, 10:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HttpContext lets you get the info if you don't have a Page class reference (this). It's the same info either way. I prefer using the Page class reference if I have one.

I would expect HttpContext.Current.User to be valid in a Master page? Maybe I have to look into this...

Eric

 
Old August 16th, 2006, 02:07 PM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to vantoko
Default

It depends on where you are using the statement.
When using it in a user control this. does not work.

I prefer to use the HTTPContect in stead of the this.User (which depends ons where you use the statement (control, page, masterpage, ...)

koen

 
Old August 17th, 2006, 08:12 AM
Authorized User
 
Join Date: May 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmmm... HttpContext.Current.User seems to be working now. Too many beers I guess :-)

Actually, what I was trying to do was redirect automatically to the default admin page when an admin logged in using the Logged_In event of a login control:

    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.IsInRole("admin")
            Response.Redirect("admin/default.aspx");
    }

... but doesn't seem to work. Debugging shows HttpContext.Current.User.Identity.Name to be an empty string.

Can someone suggest what I am doing wrong?


 
Old August 18th, 2006, 01:13 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to vantoko
Default

Hi,

you should first check if the user is authenticated :
HttpContext.Current.User.Identity.IsAuthenticated

Koen






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
Setting logged in UserName to Session ID Admiral1701 ASP.NET 2.0 Professional 10 December 1st, 2006 10:35 AM
user logged jonyBravo Access 6 November 27th, 2006 09:14 AM





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