ASP.NET default login, getting user ID session value

pastapockets picture pastapockets · May 22, 2010 · Viewed 7.5k times · Source

I've used the built-in wizard in Visual Web Developer 2008 to create a simple login system.

I'd like to get hold of the logged in user's ID, but I'm not sure how. Peeking in the ASPNETDB.MDF in the table aspnet_Users, the column appears to be called "UserId".

I gave it a go:

Response.Write("ID: " + Session["UserId"]);

but it's coming up blank.

How do I do this?

(This is not for a live project, no need to point out the sillyness in using the wizard.)

Thanks

Answer

wsanville picture wsanville · May 22, 2010

You can access the currently logged in user from a web page by using:

User.Identity.Name

Or, from a class in your web page, you can use (from the System.Web namespace):

HttpContext.Current.User.Identity.Name