Ayman
Ayman

Reputation: 21

ASP.Net Core Identity - Manage other users

I have customized the default user identity and created several users. assuming that i am the admin and i want to manage other users information (Change property values). how can i get access to other users data if i have their IDs.

i can retrieve all users in a table and get their IDs in "asp-route-id"

    @foreach (var item in Model.ApplicationUser)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage" asp-route-id="@item.Id">Edit</a>

I am stuck when using userManager to retrieve\Updating the other user information in Get & Post methods.

Upvotes: 1

Views: 730

Answers (1)

Ayman
Ayman

Reputation: 21

i managed to solve it using lambda expression

var UsertoEdit = _userManager.Users.FirstOrDefault(u => u.Id == ID);

the ID should be obtained by asp-route

i have posted the below code for reference:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WebApplication2.Models;

namespace WebApplication2.Areas.Identity.Pages.Account.Manage
{

    public partial class IndexModel : PageModel
    {
        private readonly UserManager<ApplicationUser> _userManager;
        //private readonly SignInManager<ApplicationUser> _signInManager;

        public IndexModel(
            UserManager<ApplicationUser> userManager)

        {
            _userManager = userManager;

        }



        [BindProperty]
        public InputModel Input { get; set; }



        public class InputModel
        {

            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Username { get; set; }


        }

        private async Task LoadAsync(string ID)
        {


            var UsertoEdit = _userManager.Users.FirstOrDefault(u => u.Id == ID);



            Input = new InputModel
            {
                FirstName = UsertoEdit.FirstName,
                LastName = UsertoEdit.LastName,
                Username = UsertoEdit.UserName
            };
        }

        public async Task<IActionResult> OnGetAsync(string ID)
        {
            var UsertoEdit = _userManager.Users.FirstOrDefault(u => u.Id == ID);

            if (UsertoEdit == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }



            await LoadAsync(ID);
            return Page();

        }

        public async Task<IActionResult> OnPostAsync(string ID)
        {

            var UsertoEdit = _userManager.Users.FirstOrDefault(u => u.Id == ID);

            if (UsertoEdit == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (!ModelState.IsValid)
            {

                await LoadAsync(UsertoEdit.UserName);
                return Page();
            }
            if (Input.FirstName != UsertoEdit.FirstName)
            {
                UsertoEdit.FirstName = Input.FirstName;
            }

            if (Input.LastName != UsertoEdit.LastName)
            {
                UsertoEdit.LastName = Input.LastName;
            }
            await _userManager.UpdateAsync(UsertoEdit);

            return RedirectToPage("/Users");
        }
    }
}

Upvotes: 1

Related Questions