Search code examples
c#asp.net-mvcasp.net-identity

Is there a way to view other application users profiles in asp.net?


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.


Solution

  • 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... 
                  };