Get ASP.NET Identity Current User In View

后端 未结 4 1556
無奈伤痛
無奈伤痛 2020-12-30 05:01

I use ASP.NET Identity 2.0 and MVC. I need to logged user\'s name,surname,email etc.. in view. How can get it? I can get just @User.Identity but there no my user class\'s pr

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 05:46

    In case you using .NET Core app:

    You can easily use the @inject feature call for injecting both UserManager and SignInManager.

    In your view add the following:

    @inject SignInManager SignInManager
    @inject UserManager UserManager
    

    After the injection, you should be able to work with UserManager and SignInManager methods. For instance:

    @if (SignInManager.IsSignedIn(User))
    {
        Hello @UserManager.GetUserName(User)
    }
    else
    {
    }
    

    Pay attention for passing the User object when you need to reference the current logged in user.

提交回复
热议问题