Identity用戶管理入門二(顯示用戶列表)


在Controllers中新建AccountController,並在構造方法(函數)中注入SignInManager,UserManager

UserManager   用戶管理(注冊,查找,修改,刪除用戶等)

SignInManager 用戶登錄管理(登錄,注銷等)

private readonly SignInManager<IdentityUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager;

public AccountController(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
{
    _signInManager = signInManager;
    _userManager = userManager;
}

創建Index方法顯示所有用戶

public async Task<IActionResult> Index()
{
    var user = await _userManager.Users.ToListAsync();
    return View(user);
}

創建Index.cshtml視圖

@model IEnumerable<Microsoft.AspNetCore.Identity.IdentityUser>
@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>
<a asp-action="Register">注冊</a>
<table class="table table-bordered">
    <tr>
        <th>@Html.DisplayNameFor(u => u.Id)</th>
        <th>@Html.DisplayNameFor(u => u.UserName)</th>
        <th>@Html.DisplayNameFor(u => u.Email)</th>
        <th>@Html.DisplayNameFor(u => u.PhoneNumber)</th>
        <th>操作</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.UserName</td>
            <td>@item.Email</td>
            <td>@item.PhoneNumber</td>
            <td><a asp-action="EditUser" asp-route-id="@item.Id" type="button" class="btn btn-success btn-xs">修改</a>
            <a asp-action="Delete" asp-route-id="@item.Id" type="button" class="btn  btn-danger btn-xs" size="">刪除</a> </td>
        </tr>
    }
</table>

顯示結果如下(默認沒有任何用戶,但不方便展示效果故手工新增數據)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM