Template
mirror of
https://github.com/egmont11/ASP.Net-Core-MVC-Template.git
synced 2026-07-24 07:09:56 +02:00
Changed Auth to use Identity, with easy to set passsword requirenments via appsettings.json
This commit is contained in:
@@ -1,28 +1,37 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TemplateWeb.Data;
|
||||
using TemplateWeb.Entities;
|
||||
using TemplateWeb.Models;
|
||||
|
||||
namespace TemplateWeb.Areas.Admin.Controllers;
|
||||
|
||||
public class UsersController : AdminBaseController
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
private readonly UserManager<UserEntity> _userManager;
|
||||
|
||||
public UsersController(AppDbContext context)
|
||||
public UsersController(UserManager<UserEntity> userManager)
|
||||
{
|
||||
_context = context;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var users = await _context.Users.ToListAsync();
|
||||
return View(users);
|
||||
var users = _userManager.Users.ToList();
|
||||
var viewModels = new List<UserWithRolesViewModel>();
|
||||
foreach (var user in users)
|
||||
{
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
viewModels.Add(new UserWithRolesViewModel { UserEntity = user, Roles = roles });
|
||||
}
|
||||
return View(viewModels);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Details(int id)
|
||||
public async Task<IActionResult> Details(string id)
|
||||
{
|
||||
var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == id);
|
||||
var user = await _userManager.FindByIdAsync(id);
|
||||
if (user == null) return NotFound();
|
||||
return View(user);
|
||||
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
return View(new UserWithRolesViewModel { UserEntity = user, Roles = roles });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
@model UserModel
|
||||
@model UserWithRolesViewModel
|
||||
@{
|
||||
ViewData["Title"] = "User Details";
|
||||
}
|
||||
@@ -9,22 +9,25 @@
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header bg-primary text-white">
|
||||
<h3 class="card-title mb-0">User Information: @Model.UserName</h3>
|
||||
<h3 class="card-title mb-0">User Information: @Model.UserEntity.UserName</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<dl class="row">
|
||||
<dt class="col-sm-3">ID</dt>
|
||||
<dd class="col-sm-9">@Model.Id</dd>
|
||||
<dt class="col-sm-3">ID</dt>
|
||||
<dd class="col-sm-9">@Model.UserEntity.Id</dd>
|
||||
|
||||
<dt class="col-sm-3">Username</dt>
|
||||
<dd class="col-sm-9">@Model.UserName</dd>
|
||||
<dd class="col-sm-9">@Model.UserEntity.UserName</dd>
|
||||
|
||||
<dt class="col-sm-3">Email</dt>
|
||||
<dd class="col-sm-9">@Model.Email</dd>
|
||||
<dd class="col-sm-9">@Model.UserEntity.Email</dd>
|
||||
|
||||
<dt class="col-sm-3">Role</dt>
|
||||
<dt class="col-sm-3">Roles</dt>
|
||||
<dd class="col-sm-9">
|
||||
<span class="badge @(Model.Role == UserRole.Admin ? "bg-danger" : "bg-info")">@Model.Role</span>
|
||||
@foreach (var role in Model.Roles)
|
||||
{
|
||||
<span class="badge @(role == "Admin" ? "bg-danger" : "bg-info")">@role</span>
|
||||
}
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
@model IEnumerable<UserModel>
|
||||
@model IEnumerable<UserWithRolesViewModel>
|
||||
@{
|
||||
ViewData["Title"] = "User Management";
|
||||
}
|
||||
@@ -13,20 +13,25 @@
|
||||
<th>ID</th>
|
||||
<th>Username</th>
|
||||
<th>Email</th>
|
||||
<th>Role</th>
|
||||
<th>Roles</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var user in Model)
|
||||
@foreach (var vm in Model)
|
||||
{
|
||||
<tr>
|
||||
<td>@user.Id</td>
|
||||
<td>@user.UserName</td>
|
||||
<td>@user.Email</td>
|
||||
<td><span class="badge @(user.Role == UserRole.Admin ? "bg-danger" : "bg-info")">@user.Role</span></td>
|
||||
<td>@vm.UserEntity.Id</td>
|
||||
<td>@vm.UserEntity.UserName</td>
|
||||
<td>@vm.UserEntity.Email</td>
|
||||
<td>
|
||||
<a asp-action="Details" asp-route-id="@user.Id" class="btn btn-sm btn-outline-primary">Details</a>
|
||||
@foreach (var role in vm.Roles)
|
||||
{
|
||||
<span class="badge @(role == "Admin" ? "bg-danger" : "bg-info")">@role</span>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
<a asp-action="Details" asp-route-id="@vm.UserEntity.Id" class="btn btn-sm btn-outline-primary">Details</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
@using TemplateWeb
|
||||
@using TemplateWeb.Models
|
||||
@using TemplateWeb.Models.DbModels
|
||||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
||||
|
||||
Reference in New Issue
Block a user