locked
Net Core 3 Identity RRS feed

  • Question

  • User-224304729 posted

    Hey everyone. I'm new to asp net core and i need some help. I scaffolded the Identity into my  web form razor project. The database name is ProiectLicenta where i can see all the Indentity tables. Basically what i am trying to do is if anyone registers i want their usernames and emails from the AspNetUsers table listed in another page called userlist. That table on the userlist has to update everytime a new person registers. Is it posible?

    Tuesday, May 5, 2020 12:00 AM

All replies

  • User-854763662 posted

    Hi VasiloaeaStefan ,

    I scaffolded the Identity into my  web form razor project.

    ASP.NET Core team has decided not to support web forms. You web form razor project is a asp.net core razor pages project ?

    For viewing the all registered user list, you could get them from UserManager<IdentityUser>  as shown:

    Page behind-code

    public class UserListModel : PageModel
    {
            private readonly UserManager<IdentityUser> _userManager;
    
            public UserListModel(UserManager<IdentityUser> userManager)
            {
                _userManager = userManager;
            }
    
            public List<IdentityUser> UserList { get; set; }
            public void OnGet()
            {
                UserList = _userManager.Users.ToList();
            }
    }

    Razor Pages

    @page
    @model RazorPagesIdentityDemo3_1.UserListModel
    @{
        ViewData["Title"] = "UserList";
    }
    
    <h1>UserList</h1>
    
    <table class="table">
        <thead>
            <tr>
                <th>
                    UserName
                </th>
                <th>
                    Email
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @for(var i=0;i<Model.UserList.Count;i++)
            {
                <tr>
                    <td>
                        @Model.UserList[i].UserName
                    </td>
                    <td>
                        @Model.UserList[i].Email
                    </td>
                    <td>
                        <a asp-action="Edit" asp-route-id="@Model.UserList[i].Id">Edit</a> |
                        <a asp-action="Details" asp-route-id="@Model.UserList[i].Id">Details</a> |
                        <a asp-action="Delete" asp-route-id="@Model.UserList[i].Id">Delete</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    Result

    Best Regards,

    Sherry

    Tuesday, May 5, 2020 2:27 AM
  • User-224304729 posted

    Thank you for your help and I'm sorry bothering you. Now I'm trying to use the delete function with the onpost page handler. I tried something but I don't know where i am going wrong here. The list of the users does appear, just can't get the delete function to work.

    .cshtml.cs

        public class IndexModel : PageModel
        {
            private readonly UserManager<IdentityUser> _userManager;
    
            public IndexModel(UserManager<IdentityUser> userManager)
            {
                _userManager = userManager;
            }
            public List<IdentityUser> UserList { get; set; }
            public void OnGet()
            {
                UserList = _userManager.Users.ToList();
            }
            public async Task<ActionResult> OnPostDel(string id)
            {
                UserList = _userManager.Users.ToList();
                var User = await _userManager.FindByIdAsync(id);
                if (User != null)
                {
                    IdentityResult result = await _userManager.DeleteAsync(User);
                }
                return RedirectToPage("Index");
            }
        }
    }

    .cshtml

    ...
                    <td>
                        <a asp-action="Edit" asp-route-id="@Model.UserList[i].Id">Edit</a> |
                        <a asp-action="Details" asp-route-id="@Model.UserList[i].Id">Details</a> |
                        <a asp-page-handler="Del" asp-route-id="@Model.UserList[i].Id">Delete</a>
                    </td>
    ...




    Wednesday, May 6, 2020 10:50 PM
  • User-224304729 posted
    I'm still strugling with this. Can anyone give ne a heads up on how i can configure the Delete button so it will delete the user from the AspNetUsers database?
    Thursday, May 7, 2020 9:26 PM
  • User-854763662 posted

    Hi VasiloaeaStefan ,

    The param of HttpPost request is applied to body, as form param, not as query params. So change your handler to OnGet as shown:

    public async Task<ActionResult> OnGetDel(string id)
    {
                UserList = _userManager.Users.ToList();
                var User = await _userManager.FindByIdAsync(id);
                if (User != null)
                {
                    IdentityResult result = await _userManager.DeleteAsync(User);
                }
                return RedirectToPage("Index");
    }

    Best Regards,

    Sherry

    Saturday, May 9, 2020 9:10 AM