سؤال

In an MVC5 application how do you get values from the AspNetUsers table for the current user?

For example: one of the default fileds is PhoneNumber. How do you get the phonenumber of the current logged in user?

I am using Identity 2...

هل كانت مفيدة؟

المحلول

You need to get the IdentityUser object (probably a descendant like ApplicationUser in your application) from Entity Framework. There are a number of ways you could do this, depending on where you are and so on. But, for example, if you wanted to do that in the controller, where you have access to the currently logged in user with the User property, you could use UserManager<TUser>.FindById() like this:

// UserManager here is an instance of UserManager<ApplicationUser>
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
var number = user.PhoneNumber;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top