Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,


i want to get the name of the user who is logged in from my login page. I use LoginName control but it displays the name of my windows user name
Posted
Comments
AmitGajjar 18-Aug-12 1:31am    
you need to learn use of Session variables. you need to persist your username though out the active session.

C#
WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
// or, if you're in Asp.Net with windows authentication you can use:
// WindowsPrincipal principal = (WindowsPrincipal)User;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal up = UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
    return up.DisplayName;
    // or return up.GivenName + " " + up.Surname;
}
 
Share this answer
 
v2
Comments
Pradeep_kaushik 18-Aug-12 1:19am    
is any liberary required for it ?

it gives an error

"Object reference not set to an instance of an object." on line label.text-----
Santhosh Kumar Jayaraman 18-Aug-12 1:26am    
Check the updated solution
Try
C#
User.Identity.Name


as given below
C#
if (User.Identity.IsAuthenticated)
    LabelUName.Text = User.Identity.Name;
else
    LabelUName.Text = "No user identity available.";
 
Share this answer
 
Simple way is use session.


create session for username
for ex: session["UserName"]


you can also create multiple session, and use in all page of current project.



accept if ans. is right.
 
Share this answer
 
Comments
ridoy 18-Aug-12 2:24am    
it's right
[no name] 18-Aug-12 2:27am    
Accept Solution...!!
Pradeep_kaushik 18-Aug-12 2:35am    
thanx...
[no name] 18-Aug-12 3:02am    
@All, Welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900