简体   繁体   中英

How to get current user logged in MVC 3?

In my project, when user login, I had use

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

and in I need get current logged in user to get user's role. I don't know how to do that. Any one help me?

In your controller/action you can access this member variable to get the name

var x=HttpContext.User.Identity.Name;

I use this name to then look up and see if they are in role, depending on how you have implemented the Identity part and if you have a role provider you can also do things like this

var isAdmin=HttpContext.User.IsInRole("administrator");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM