开发者

What is the best way to identify logged user?

开发者 https://www.devze.com 2023-04-06 21:16 出处:网络
I was looking for the efficient way to track the logged users when using as开发者_高级运维p.net login control

I was looking for the efficient way to track the logged users when using as开发者_高级运维p.net login control

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

Any suggestions will be appreciated


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?


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

HttpContext.Current.User.Identity.Name


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


You can just simply use

User.Identity.Name


You can use User.Identity.Name

Please have a look at HttpContext.User Property

0

精彩评论

暂无评论...
验证码 换一张
取 消