group user list by admin state

Strosala Ioan 61 Reputation points
2022-11-13T08:08:06.787+00:00

Dear all. I started one project in asp.net core which have administrator account by state. I use identity where changed IdentityUser as follow:

public class ApplicationUser : IdentityUser  
    {  
        public string? FullName { get; set; }  
        public string? State { get; set; }  
    }  

The admin controllers contains:

[Authorize("Authorization")]  
        public async Task<IActionResult> Users()  
        {  
            var userViewModel = new List<UserViewModel>();  
            var users = await _userManager.Users.ToListAsync();  
            foreach (var item in users)  
            {  
                userViewModel.Add(new UserViewModel()  
                {  
                    Id = item.Id,  
                    Email = item.Email,  
                    UserName = item.UserName,  
                    State = item.State,  
                });  
            }  
  
            return View(userViewModel);  
        }  

In this view, I want to obtain a list of users according to the administrator who manages that region. For example, when the administrator from Alabama logs in, he should only see users from that state
The _LoginPartial.chtml page I display FullName and State:

<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown" data-bs-auto-close="outside">Hello @(User.FindFirst("UserFullName")?.Value + " " + User.FindFirst("UserState")?.Value)!</a>  

Please help me get the list of users according to the administrator of each state.

Thank you!

Developer technologies | ASP.NET | ASP.NET Core
Microsoft Security | Microsoft Identity Manager
{count} votes

Accepted answer
  1. Anonymous
    2022-11-14T07:37:34.21+00:00

    Hi @Strosala Ioan ,

    In this view, I want to obtain a list of users according to the administrator who manages that region. For example, when the administrator from Alabama logs in, he should only see users from that state

    Based on your model: use the ApplicationUser to store the state, since the administrator also use the ApplicationUser to store his information, in the Users method, you can get current user's state, then use this state to filter the Users table.

    The Users method code like this:

             [Authorize("Authorization")]  
             public async Task<IActionResult> Users()  
             {  
                 //find current user's state  
                var currentuserstate = _userManager.FindByNameAsync(User.Identity.Name).Result.State;  
                //based on the state to filter the data.   
                var users = await _userManager.Users.Where(c => c.State == currentuserstate).Select(c => new UserViewModel()  
                {  
                    Id = c.Id,  
                    Email = c.Email,  
                    UserName = c.UserName,  
                    State = c.State,  
                    UserNameandState = $"{c.UserName} - {c.State}"  
                }).ToListAsync();  
      
                 return View(userViewModel);  
             }  
    

    The UserViewModel like this:

    public class UserViewModel   
    {  
        public string Id { get; set; }  
        public string Email { get; set; }  
        public string UserName { get; set; }  
    
        public string State { get; set; }  
    
        public string UserNameandState { get; set; }  
    }  
    

    Then you can use the UserNameansState property to display the name and the state, the result like this:

    AspNetUsers table:

    259918-image.png

    The view result:

    260022-image.png

    Besides, you can also create State model and configure one-to-many relationship with the ApplicationUser table, like this:

    public class ApplicationUser:IdentityUser   
    {  
        public string? FullName { get; set; }  
        //public string? State { get; set; }  
    
        [ForeignKey("State")]  
        public int StateId { get; set; }  
        public State State { get; set; }  
    }  
    
    public class State  
    {  
        public int StateId { get; set; }  
        public string StateName { get; set; }  
        public string StateCode { get; set; }  
        public List<ApplicationUser> ApplicationUsers { get; set; }  
    }  
    

    Then, after migration and add data, the database like this:

    260041-image.png

    In the controller, you can query the database via the ApplicationDbContext, use the SelectMany method or Left-join to query the AspNetUsers and the States table. The query statement like this:

             [Authorize("Authorization")]  
             public async Task<IActionResult> Users()  
             {  
                 //find current user's state  
                var currentuserstate = _userManager.FindByNameAsync(User.Identity.Name).Result.StateId;  
                //based on the state to filter the data.   
                var result = await _context.States.Include(c => c.ApplicationUsers).Where(c=>c.StateId == currentuserstate).SelectMany( x => x.ApplicationUsers.DefaultIfEmpty(), (s, u) =>   
                new UserViewModel()  
                {  
                    Id = u.Id,  
                    UserName = u.UserName,  
                    Email = u.Email,  
                     UserNameandState =$"{u.UserName} - {s.StateCode}"  
                }).ToListAsync();  
     
                 return View(userViewModel);  
             }  
    

    By using the above query statement, we can also get the list of users based on current user's state. the output like this:

    260051-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


1 additional answer

Sort by: Most helpful
  1. Strosala Ioan 61 Reputation points
    2022-11-13T12:30:41.587+00:00

    Mr. AgaveJoe, Please help me with an example.

    At this moment I use the AspNetUsers table consists of the standard fields plus FullName and State. After authentication, I display the state of which each user belongs (eg. jhon.doe.al - al= Alabama, mary.michael.ia - ia -Iowa)

    Thank you!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.