Defining a User with User.Identity.Name in controller constructor

前端 未结 4 1753
终归单人心
终归单人心 2021-02-03 13:15

For my actions that are going to interact with the User\'s account, I would like to create a \"TheUser\" object in addition to adding that object to \"ViewData[\"TheUser\"]\" as

4条回答
  •  遥遥无期
    2021-02-03 13:48

    public abstract class YourController : Controller
    {
        public YourController()
        {
            var user = System.Threading.Thread.CurrentPrincipal;
    
            TheUser = User.Identity.IsAuthenticated ? UserRepository.GetUser(user.Identity.Name) : null;
    
            ViewData["TheUser"] = TheUser;  
        }
    }
    

提交回复
热议问题