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.
Asked
Active
Viewed 1.4k times
0
-
Did you check the answers? Are you still running into issues? – Ravi A. Sep 28 '16 at 12:37
5 Answers
2
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

Heemanshu Bhalla
- 3,603
- 1
- 27
- 53

Haitham Shaddad
- 4,336
- 2
- 14
- 19
-
don't know why but in my case Findbyid is giving null. i think you are giving name over there. you should give id over there – Heemanshu Bhalla Feb 07 '20 at 08:10
-
@HeemanshuBhalla it depends on your user id, usually you have an id (GUID) – Haitham Shaddad Feb 08 '20 at 10:43
0
User.Identity.GetUserName();
will give you current logged in user's email address.You should check default MVC template for more help.

Ravi A.
- 2,163
- 2
- 18
- 26
0
If you have a filed for "email" in your ApplicationUser you can access it without a query getting from claims, eg in controllers you can:
this.User.GetClaimValue("email")

fiorebat
- 3,431
- 2
- 18
- 18