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>
复制代码

显示结果如下(默认没有任何用户,但不方便展示效果故手工新增数据)

posted @   liessay  阅读(1135)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示