简体   繁体   中英

How to get currently logged user's email address to the View in ASP.NET c#?

I'm developing an asp.net application using c# according to mvc. In there i'm creating a user profile which can be edit or deactivate to the current logged user. In my user profile view i have a field for the email. So that i need to get current logged user's email address to that field (input tag) in View file. How to get the currently logged user's email address to a View file? And I have used Identity model.

It depends on your code, are you using the email to login or a username? If you are using the email, then call the User.Identity.Name to get it.

If you are using a username, then use the same line to get the username then call the UserManager class to get the User object using the username and then use the email property -

UserManager.FindById(User.Identity.GetUserId()); 

Put the email in a ViewBag, ex: ViewBag.Email = user.Email.

Read that value in the view @ViewBag.Email

As of MVC 5 this is how you do it based on this SO question :

var user = UserManager.FindById(User.Identity.GetUserId());
var email = user.Email;

User.Identity.GetUserName(); will give you current logged in user's email address.You should check default MVC template for more help.

如果你真的使用身份模型,只需在视图中调用它:@User.Identity.GetUserName()

如果您在 ApplicationUser 中有一个“电子邮件”申请,您可以访问它而无需从声明中获取查询,例如在控制器中,您可以:

this.User.GetClaimValue("email")

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