0

Im curious when using Identity and saving the user info inside aspnetUsers table in the Default EF framework database. Is there a way to render the properties to a view that can be viewed by other Users that are all on at the same time when the app is running. Im having an issue where Im just getting the current user. I need to build a user search sub system that can display users and have a link to their profiles.

For example I added properties to my ApplicationUser in Identity.Models

public` string displayName { get; set; }

public string age { get; set; }

public string description { get; set; }

in my Home controller(I know its in about but im just testing to see if it works)

public ActionResult About() {

        // Instantiate the ASP.NET Identity system
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
        var currentUser = manager.FindById(User.Identity.GetUserId());

        // Recover the profile information about the logged in user
        ViewBag.displayName = currentUser.displayName;
        ViewBag.age = currentUser.age;
        ViewBag.description = currentUser.description;



        ////////
        var usr = User.Identity.Name;
       // var usrName = ;
        ViewBag.Message = usr;

        return View();
    }

In my About view

@using Microsoft.AspNet.Identity;
@{
    ViewBag.Title = "About";

}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<h3>@ViewBag.displayName</h3>
<h3>@ViewBag.age</h3>
<h3>@ViewBag.description</h3>

I can see the current user properties but im trying to figure out How asp.net interacts with other Application users to view other profiles? I know the program is doing what im asking it to do(placing the properties from the current user in the about view) but I kind of have no Idea of where to go next.

Mohamoud Mohamed
  • 515
  • 7
  • 16
  • You can use a ViewModel for your profile fill it with the information you want to show and send it with view. – Masoud Andalibi Feb 18 '17 at 10:31
  • Thanks im a little green at this. So I would pass the information via a viewmodel would that give each user a unique url? would each user on the app their own viewModel. If 2 people where on at the same time could they view the other's viewModel or would it only have their currentUser info? – Mohamoud Mohamed Feb 18 '17 at 11:00

1 Answers1

1

Create a ViewModel Lets call it ProfileViewModel this is what you are going to show the user in their profile so fill it with properties that you wish you show the user(be sure if you have any extra property it has to be added to the ApplicationUser as well), now one user clicks on its profile page and wants to see his profile, using identity you can get its userId or you have stored it in a session or cookie so if 2 user at sametime clicked the profile each one has their own profile shown not each other (User.Identity.GetUserId()) gives you the userId.

Now in your ActionResult what you need to do is to get an entity of ApplicationUser based on its UserId and Fill the ProfileViewModel from the ApplicationUser and pass it over to your View.

this is how you get current ApplicationUser:

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

And how to fill your ProfileViewModel:

var profile = new ProfileViewModel
              {
                 UserName = user.Username,
                 //so on... 
              };
Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44
  • Ok I am going to try this. But wouldn't this carry the same information to the profileVeiwModel (only showing the currently logged in user their properties but in a profileViewModel instead of the AboutView)kind of doing the same thing I already have? Lets say there is User A and User B. User A wants to view user B's profileViewModel and vice versa. Thanks btw – Mohamoud Mohamed Feb 18 '17 at 12:00
  • @fasecity in that case you need to create a list of ApplicationUser in a view for example, whenever you click on a user it has to send its id to your actionresult using for example `querystring` then you have an id, you can create the entity using `FindById` and fill the ProfileViewModel based on that entity and show it, – Masoud Andalibi Feb 18 '17 at 12:04
  • @fasecity this is [how you get a list of your users](http://stackoverflow.com/questions/21505592/getting-all-users-and-all-roles-through-asp-net-identity) – Masoud Andalibi Feb 18 '17 at 12:13
  • Hey I cant seem to iterate through the ApplicationUsers using the information in that link. My brain is dead so im gonna go to sleep and hopefully figure it out later. Thanks for your help. I was looking for an answer like this with a querystring. – Mohamoud Mohamed Feb 18 '17 at 12:53