1

I was looking for the efficient way to track the logged users when using asp.net login control

        if (!IsPostBack)
        {
            if (Membership.GetUser() != null)
            {
                var user = Membership.GetUser();
                Session["user"] = user;
            }
            else
            {
                Session["user"] = "";
            }
        }

Any suggestions will be appreciated

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

5 Answers5

3

You can get the user identity (if you're using asp.net forms membership) through:

HttpContext.Current.User.Identity.Name
AndrewC
  • 6,680
  • 13
  • 43
  • 71
3

why all this pain and why do you try to save it in the Session (which is user specific and not application specific), when you can simply get it from this object:

HttpContext.Current.User

check this one for details: How to get current user who's accessing an ASP.NET application?

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
2

On logging in, if the user is valid, use:

FormsAuthentication.SetAuthCookie(login.UserName, false);

rather than relying on Session to store user logged in state.

You can then check if a user is logged in by:

User.Identity.IsAuthenticated

And get their username doing:

User.Identity.Name
Chris W
  • 1,792
  • 15
  • 32
1

You can use User.Identity.Name

Please have a look at HttpContext.User Property

1

You can just simply use

User.Identity.Name
Pleun
  • 8,856
  • 2
  • 30
  • 50