0

I am using Asp.Net built-in membership framework , I have a requirement where I need to display the name of the logged-in user, my aspnet_users table stores the user name something like this abc001 which is actually the usercode of the user , its user name is stored in another table which is linked to the aspnet_users table with a foreign key constraint.So how do I display the name of the user from that table in my navigation menu which will be floating towards the right.Much like Facebook does , it displays logged-in users name in the header. Edit:

protected void cmdLogin_Click(object sender, EventArgs e)
        {
            if (Membership.ValidateUser(txtUsername.Text, txtPassword.Text))
            {
                HttpCookie authCookie = FormsAuthentication.GetAuthCookie(txtUsername.Text, false);
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, "");
                authCookie.Value = FormsAuthentication.Encrypt(newTicket);
                Response.Cookies.Add(authCookie);
                GetUserName(); 
                string redirUrl = FormsAuthentication.GetRedirectUrl(txtUsername.Text, false);
                Response.Redirect(redirUrl);

            }


        }
        public void GetUserName()
        {
            DataClasses1DataContext dt = new DataClasses1DataContext();
            var name =( from nm in dt.Users_AdditionalInfos
                        where nm.UserCode== txtUsername.Text.Trim() 
                        select nm).Single();
           Global.UserName=name.FirstName +" "+ name.LastName; 
        }

Thanks

3 Answers3

1

You can use,

Membership.GetUser().UserName

...to get the currently logged in user. If the user is not logged in, you will get a null value returned by GetUser(), so code accordingly.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • but the name is actually stored in a different table which is linked to the built-in table so I think Membership.GetUser().UserName wont give me the name. – priyank patel May 03 '12 at 11:49
0

Try User.Identity.Name

See more about HttpContext.User Property

If you need to get a different information from another table you can get the username and do something like this

MembershipUser userInfo = Membership.GetUser("myUserName");    
Guid guid = new Guid(userInfo.ProviderUserKey.ToString()); 

ProviderUserKey is unique for each user and I guess you using that as the foreign key.Since you have ProviderUserKey you can get necessary details from your other table

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
0

User.Identity.Name can be used to access the username of the user. Have a look at this tutorial as it further explains how you can get membership information of user. http://mvctutorialsandguides.blogspot.co.nz/2013/03/mvc-c-sql-membership-provider-how-to.html Hope this helps.