I want to access the login name of a user who is currently logged in my program and store this information in a database. How do I do this? I currently use login controls (such as loginview, etc.).
As Mudasir.Khan suggested, you can use HttpContext.Current.User.Identity to get the authenticated user information. And this works if you're using ASP.NET built-in authentication methods (such as Windows, Forms authentications) for your web application.
So when using built-in forms authentication, after user logon via the login.aspx page, the authenticated user identity info is stored in cookie and available through the HttpContext.Current object.
If you use your custom forms authentication such as manually do the username/password validation and use a session variable to mark the authentication status of current request, then you need to use your custom code logic to determine current user.
BTW, if you have interests, you can use reflector tool to inspect how the LoginView or LoginName control is implemented for more ideas.
aussiejmasp
Member
19 Points
110 Posts
Accessing the login name of a user who is logged in
May 21, 2012 12:12 PM|LINK
Hi,
I want to access the login name of a user who is currently logged in my program and store this information in a database. How do I do this? I currently use login controls (such as loginview, etc.).
rajesh134
Member
37 Points
42 Posts
Re: Accessing the login name of a user who is logged in
May 21, 2012 12:15 PM|LINK
dtore the username in sessions which can be accessable throught the application
aussiejmasp
Member
19 Points
110 Posts
Re: Accessing the login name of a user who is logged in
May 21, 2012 12:25 PM|LINK
How do I do that?
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Accessing the login name of a user who is logged in
May 21, 2012 12:26 PM|LINK
System.Web.HttpContext.Current.User.Identity.Name
venugopaldik...
Member
44 Points
26 Posts
Re: Accessing the login name of a user who is logged in
May 21, 2012 05:24 PM|LINK
Yes its right way to get the username of the logged-in user from the login control based authentication. Follow the same.
India.
Steven Cheng...
Contributor
4219 Points
548 Posts
Microsoft
Moderator
Re: Accessing the login name of a user who is logged in
May 22, 2012 03:54 AM|LINK
Hi aussiejmasp,
As Mudasir.Khan suggested, you can use HttpContext.Current.User.Identity to get the authenticated user information. And this works if you're using ASP.NET built-in authentication methods (such as Windows, Forms authentications) for your web application.
So when using built-in forms authentication, after user logon via the login.aspx page, the authenticated user identity info is stored in cookie and available through the HttpContext.Current object.
#Explained: Forms Authentication in ASP.NET 2.0 h
ttp://msdn.microsoft.com/en-us/library/ff647070.aspx
If you use your custom forms authentication such as manually do the username/password validation and use a session variable to mark the authentication status of current request, then you need to use your custom code logic to determine current user.
BTW, if you have interests, you can use reflector tool to inspect how the LoginView or LoginName control is implemented for more ideas.
Feedback to us
Microsoft One Code Framework